Java面向?qū)ο箢惡蛯ο髮嵗斀?/h1>
更新時間:2022年04月06日 16:55:10 作者:賴正華
面向?qū)ο竽耸荍ava語言的核心,是程序設(shè)計的思想,這篇文章主要介紹了Java面向?qū)ο箢惡蛯ο蟮南嚓P(guān)資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下
1 - Java面向?qū)ο髮W(xué)習(xí)的三條主線
①Java類即類的成員:屬性 方法 構(gòu)造器 (代碼塊 內(nèi)部類)
②面向?qū)ο蟮娜筇卣鳎悍庋b性 繼承性 多態(tài)性 (抽象性)
③其他關(guān)鍵字:this supper static final abstract interface 等
2 - 面向過程與面向?qū)ο?/h2>
二者都是一種思想,面向?qū)ο笫窍鄬τ诿嫦蜻^程而言的
1-面向過程:強調(diào)的是功能行為,以函數(shù)為最小單位,考慮怎么做
2-面向?qū)ο螅簩⒐δ芊庋b進對象,強調(diào)具備了功能的對象,以類/對象為最小單位,考慮誰來做
面向?qū)ο蟾訌娬{(diào)運用人類在日常的思維邏輯中采用的思想方法與原則,如抽象,分類,繼承,聚合,多態(tài)等
/*
人把大象裝進冰箱
面向過程:強調(diào)的是功能行為,以函數(shù)為最小單位,考慮怎么做
①將冰箱門打開
②抬起大象,裝進冰箱
③關(guān)閉冰箱門
面向?qū)ο螅簩⒐δ芊庋b進對象,強調(diào)具備了功能的對象,以類/對象為最小單位,考慮誰來做
人{
打開(冰箱){
冰箱.開開門();
}
抬起(大象){
大象.進入(冰箱);
}
關(guān)閉(冰箱){
冰箱.閉合();
}
}
冰箱{
開開門(){}
閉合(){}
}
大象{
進入(冰箱){}
}
*/
3 - 面向?qū)ο蟮娜筇卣?/h2>
封裝(Encapsulation)
繼承(Inheritance)
多態(tài)(Polymorphism)
4 - 面向?qū)ο蠓治龇椒ǚ治鰡栴}的思路和步驟
/*
* 程序員從面向過程的執(zhí)行者轉(zhuǎn)化成了面向?qū)ο蟮闹笓]著
* 面向?qū)ο蠓治龇椒ǚ治鰡栴}的思路和步驟:
* 1-根據(jù)問題 需要,選擇問題所針對的現(xiàn)實世界中的實體
* 2-從實體中尋找解決問題相關(guān)的屬性和功能,這些屬性和功能就形成了概念世界中的類
* 3-把抽象的實體用計算機語言進行描述,形成計算機世界中類的定義,即借助某種程序語言,把類構(gòu)造成計算機能夠識別和處理的 數(shù)據(jù)結(jié)構(gòu)
*
* 4-將類實例化成計算機世界中的對象,對象是計算機世界中解決問題的最終工具
*/
5 - 面向?qū)ο蟮乃枷敫攀?/h2>
1-類(Class)和對象(Object)是面向?qū)ο蟮暮诵母拍睿?/p>
?、兕愂菍σ活愂挛锏拿枋?,是抽象的,概念上的定義
?、趯ο笫菍嶋H存在的該類事物的每個個體,因而也稱其為實例(instance)
?、跩ava世界里,"萬物皆對象"


2-代碼示例
/*
設(shè)計類 -> 其實就是設(shè)計類的成員
屬性 = 成員變量 = field = 域、字段
方法 = 成員方法 = 函數(shù) = method
創(chuàng)建類的對象 = 類的實例化 = 實例化類
類和對象的使用(面向?qū)ο蟮乃枷雽崿F(xiàn))
1-創(chuàng)建類,設(shè)計類的成員
2-創(chuàng)建類的對象
3-調(diào)用對象的屬性或方法(對象.屬性 對象.方法)
如果創(chuàng)建了一個類的多個對象,則每個對象都獨立的擁有一套類的屬性。(非 static 的)
意味著,我們修改一個對象的屬性a,則不影響另一個對象的屬性a。
*/
// 測試類
public class PersonTest {
public static void main(String[] args){
// 創(chuàng)建Person類的對象
Person p1 = new Person();
// Scanner scanner = new Scanner(System.in);
// 調(diào)用對象屬性:對象.屬性
System.out.println(p1.age); // 22
p1.name = "howie";
p1.isMale = true;
// 調(diào)用對象方法:對象.方法
p1.eat();
p1.sleep();
p1.talk("chinese");
// *******************
Person p2 = new Person();
System.out.println(p2.name); // null 說明類的初始化默認值為 null
Person p3 = p1; // 將p1變量保存的對象空間地址值賦值給p3,導(dǎo)致p1和p3都指向了堆空間的同一個對象實體
System.out.println(p3.name); // howie
}
}
// 創(chuàng)建類,設(shè)計類的成員
class Person{
// 屬性
String name;
int age = 22;
boolean isMale;
// 方法
public void eat(){
System.out.println("人正在吃飯!");
}
public void sleep(){
System.out.println("人可以睡覺!");
}
public void talk(String language){
System.out.println("人可以說話,使用的是"+language);
}
}
6 - 類和對象的創(chuàng)建-內(nèi)存解析
1-內(nèi)存解析

2-對象內(nèi)存解析

7 - 類中屬性的聲明和使用(局部變量與成員變量的區(qū)別)
1-屬性(成員變量)vs 局部變量
* 1-相同點
* ?、俣x變量的格式:數(shù)據(jù)類型 變量名 = 變量值
* ?、谙嚷暶?,后使用
* ?、圩兞慷加衅鋵?yīng)的作用域
* 2-不同點
* ?、僭陬愔新暶鞯奈恢貌煌?br />* 屬性:直接定義在類的一對{}內(nèi)
* 局部變量:聲明在方法內(nèi),方法形參,代碼塊,構(gòu)造器形參,構(gòu)造器內(nèi)部的變量
* ②關(guān)于權(quán)限修飾符的不同
* 屬性:可以在聲明屬性時,指定其權(quán)限,使用權(quán)限修飾符。常用的權(quán)限修飾符:private , public , 缺省 , protected
* 目前,大家聲明屬性時,都使用缺省就可以(什么都不加)
* 局部變量:不可以使用權(quán)限修飾符
* ③默認初始化的值
* 屬性:類的屬性,根據(jù)其類型,都有默認初始值。
* 整型(byte、short、int、long):0
* 浮點型(float、double):0.0
* 字符型(char):0(或'\u00000')
* 布爾(boolean):false
*
* 引用數(shù)據(jù)類型(類、數(shù)組、接口):null
* 局部變量:沒有默認初始化值,意味著我們在調(diào)用局部變量之前,一定要顯示賦值,特別的,形參調(diào)用時賦值即可。
* ?、茉趦?nèi)存中加載的位置
* 屬性:堆空間(非static)
* 局部變量:棧
public class test1 {
public static void main(String[] args) {
User u1 = new User();
System.out.println(u1.name);
System.out.println(u1.age);
System.out.println(u1.isMale);
// 調(diào)用形參
u1.talk("English");
}
}
class User{
// 屬性(或成員變量)
String name;
int age;
boolean isMale;
public void talk(String language) {// 形參,也是局部變量
System.out.println("我們使用"+language+"交流");
}
public void eat() {
String foot = "米線"; // 在方法中定義的局部變量
System.out.println("云南人喜歡吃:"+foot);
}
}
⑤內(nèi)存解析需要注意兩點
1-內(nèi)存結(jié)構(gòu):棧(局部變量)、堆[new 出來的結(jié)構(gòu):對象(非 static成員變量)、數(shù)組]
2-變量:成員變量 vs 局部變量(方法內(nèi)、方法形參、構(gòu)造器內(nèi)、構(gòu)造器形參、代碼塊內(nèi))

8 - 類中方法的聲明和使用
類中方法的申明和使用
方法:描述類應(yīng)該具備的功能
比如 Math類:aqrt()\random()...
Scanner類:nexInt() ...
Arrays類:sort() \ binarySearch() \ toString() \ equals() \ ...
1-舉例
public void eat() {}
public void sleep(int hour) {}
public String getName() {}
public String getNation(String nation) {}
2-方法的聲明
權(quán)限修飾符 返回值類型 方法名(形參列表) {
方法體
}
注意:static final abstract 來修飾的方法,后面在介紹
3-說明
?、贆?quán)限修飾符:Java規(guī)定的4種權(quán)限修飾符:private(私有的) , public , 缺省 , protected
默認方法的權(quán)限修飾符先都使用public
②返回類型:有返回值 vs 沒有返回值
1-如果方法有返回值,則必須在方法聲明時,指定返回值的類型。同時,方法中,需要使用時return關(guān)鍵字來返回指定類型的變量或常量
2-如果方法沒有返回值,則方法聲明時,使用 void來表示。通常沒有返回值的方法中,就不需要使用return關(guān)鍵字了
?、鄯椒簩儆跇俗R符,遵循標識符的規(guī)則和規(guī)范,見名知意
?、苄螀⒘斜恚悍椒梢月暶?個,1個,或多個形參...格式:數(shù)據(jù)類型1 形參1,數(shù)據(jù)類型2 形參2,...
⑤方法體:方法功能的體現(xiàn)
4-return關(guān)鍵字的使用
?、偈褂梅秶菏褂迷诜椒w中
?、谧饔茫?.結(jié)束方法2.針對于有返回值類型的方法,使用 'return 數(shù)據(jù)'方法返回所要的數(shù)據(jù)。
?、圩⒁恻c:return關(guān)鍵字后面不可以聲明執(zhí)行語句
5-方法的使用:可以調(diào)用當前類的屬性或方法
特殊的:方法A中調(diào)用方法A稱作 遞歸方法
方法中不可以再定義方法
public class CustomerTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
// 客戶類
class Customer{
// 屬性
String name;
int age;
boolean isMale;
// 方法
public void eat() {
System.out.println("客戶吃飯");
}
public void sleep(int hour) {
System.out.println("客戶休息了"+hour+"個小時");
}
public String getName() {
return name;
}
public String getNation(String nation) {
String info = "我的國籍是"+nation;
return info;
}
}
練習(xí)題
/*
* 1-編寫程序,聲明一個method方法,在方法中打印一個10*8的*型矩形,在main方法中調(diào)用該方法。
* 2-修改上一個程序,在method方法中,除打印一個10*8的*矩形外形,再計算該矩形的面積,并將其作為方法返回值。
* 在main方法中調(diào)用該方法接受返回的面積值打印。
*
* 3-修改上一個程序,在method方法提供m和n兩個參數(shù),方法中打印一個m*n的*型矩形
*/
public class Exer3Test {
public static void main(String[] args) {
Exer3Test test = new Exer3Test();
// 第1題的測試
// test.method();
// 第2題的測試
// double s = test.method();
// System.out.println("面積為"+s);
// 第3題的測試
System.out.println(test.method(12,10));
}
// 第1問
// public void method(){
// for(int i = 0;i < 10;i++){
// for(int j = 0;j < 8;j++){
// System.out.print("* ");
// }
// System.out.println();
// }
// }
// 第2問
// public double method(){
// for(int i = 0;i < 10;i++){
// for(int j = 0;j < 8;j++){
// System.out.print("* ");
// }
// System.out.println();
// }
// return 10*8;
// 第3問
public int method(int n,int m){
for(int i = 0;i < n;i++){
for(int j = 0;j < m;j++){
System.out.print("* ");
}
System.out.println();
}
return n * m;
}
}
9 - 對象數(shù)組的使用
/*
* 對象數(shù)組題目
* 定義類Student,包含三個屬性:學(xué)號number(int),年級state(int),成績score(int)。創(chuàng)建20個學(xué)生對象,學(xué)號1到20
* 年級[1-6]和成績[0-100]都由隨機數(shù)確定。
* 問題1:打印出3年級(state值為3)的學(xué)生信息
* 問題2:使用冒泡排序按學(xué)生成績排序,并遍歷所有學(xué)生信息
*
* 提示:
* ①生成隨機數(shù):Math.random(),返回值類型double
* ②四舍五入:Math.round(double d),返回值類型long.
*/
public class StudentTest {
public static void main(String[] args){
// 聲明Student對象數(shù)組
Student[] students = new Student[20];
for(int i = 0;i < students.length;i++){
// 給數(shù)組元素賦值
students[i] = new Student();
// 給Student對象的屬性賦值
students[i].number = i + 1;
double value = Math.random();
students[i].state = (int)(value * (6 - 1 + 1) + 1);
double v = Math.random();
students[i].score = (int)(v * (100 + 1) + 0);
// System.out.print(students[i].score+"\t");
}
// 遍歷學(xué)生數(shù)組
for(int i = 0;i < students.length;i++){
// System.out.println(students[i].number+","+students[i].state+","+students[i].score);
// System.out.println(students[i].showInfo());
// 問題1:打印出3年級(state值為3)的學(xué)生信息
if(students[i].state == 3){
// System.out.println(students[i].showInfo());
}
}
// 問題2:使用 [冒泡排序] 按學(xué)生成績排序,并遍歷所有學(xué)生信息
for(int i = 0;i < students.length - 1;i++){
for(int j = 0;j < students.length - 1 - i;j++){
if(students[j].score > students[j+1].score){
Student temp = students[j];
students[j] = students[j+1];
students[j+1] = temp;
}
}
}
// 遍歷 排好序的 學(xué)生信息 從最高分打印
for(int i = 0;i < students.length;i++){
System.out.println(students[students.length - i-1].showInfo());
}
}
}
class Student{
int number; // 學(xué)號
int state; // 年級
int score; // 成績
public String showInfo(){
return "學(xué)號:"+number+" "+"年級:"+state+" "+"成績:"+score;
}
}
對象數(shù)組使用示例
數(shù)組對象內(nèi)存解析

10 - 理解 "萬物皆對象"
/*
* 1-在Java語言范疇中,我們都將功能,結(jié)構(gòu)等封裝到類中,通過類的實例化,來調(diào)用具體的功能結(jié)構(gòu)
* >Scanner,String
* >文件:File
* >網(wǎng)絡(luò)資源:URL
* 2-涉及到Java語言與前端HTML、后端的數(shù)據(jù)庫交互時、前后端的結(jié)構(gòu)在Java層面交互時,都體現(xiàn)為類、對象
*/
11 - 匿名對象
/*
* 匿名對象的使用
* 1-理解:我們創(chuàng)建的對象,沒有顯示的賦給一個變量名,即為匿名對象
* 2-特征:匿名對象只能調(diào)用一次
* 3-開發(fā)中使用:以下代碼示例
*/
public class PhoneTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
Phone p = new Phone();
// p = null;
System.out.println(p);
p.sendEmail();
p.playGame();
// 匿名對象
// new Phone().sendEmail();
// new Phone().playGame();
new Phone().price = 199;
new Phone().showPrice(); // 0.0
PhoneMall mall = new PhoneMall();
// mall.show(p);
// 匿名對象的使用
mall.show(new Phone());
}
}
class PhoneMall{
public void show(Phone phone) {
phone.sendEmail();
phone.playGame();
}
}
class Phone{
double price; // 價格
public void sendEmail() {
System.out.println("發(fā)送郵件");
}
public void playGame() {
System.out.println("打游戲");
}
public void showPrice() {
System.out.println("手機價格為"+price);
}
}
匿名對象的使用
12 - 方法的封裝(數(shù)組常用算法的封裝)
注意兩個Java文件在同級目錄
①在ArrayUtils.java 文件中 創(chuàng)建類并分裝方法
public class ArrayUtils {
// 求數(shù)組最大值
public int getMax(int[] array){
int max = array[0];
for(int i = 1;i < array.length;i++){
if(array[i] > max){
max = array[i];
}
}
return max;
}
// 求數(shù)組最小值
public int getMin(int[] array){
int min = array[0];
for(int i = 1;i < array.length;i++){
if(min > array[i]){
min = array[i];
}
}
return min;
}
// 求數(shù)組總和
public int getSum(int[] array){
int sum = 0;
for(int i = 0;i < array.length;i++){
sum += array[i];
}
return sum;
}
// 求數(shù)組平均值
public double getAvg(int[] array){
return getSum(array) / array.length;
}
// 反轉(zhuǎn)數(shù)組
public void reverseArray(int[] array){
for(int i = 0;i < array.length / 2;i++){
int temp = array[i];
array[i] = array[array.length - i - 1];
array[array.length - i - 1] = temp;
}
}
// 復(fù)制數(shù)組
public int[] copyArray(int[] array){
int[] newArray = new int[array.length];
for(int i = 0;i < array.length;i++){
newArray[i] = array[i];
}
return newArray;
}
// 數(shù)組排序
public void sortArray(int[] array){
/**
* 默認從小到大排
*/
for(int i = 0;i < array.length - 1;i++){
for(int j = 0;j < array.length - i -1;j++){
if(array[j] > array[j+1]){
int temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}
}
// 遍歷數(shù)組
public void print(int[] array){
for(int i = 0;i < array.length;i++){
System.out.print(array[i]+"\t");
}
}
// 查找數(shù)組指定的元素
public int getIndex(int[] array,int dest){
int head = 0;
int end = array.length - 1; // 初始的末索引
boolean isFlag = false;
int middle = 0;
sortArray(array);
while(head <= end){
middle = (head + end) / 2;
if(dest == array[middle]){
isFlag = true;
break;
}else if(dest > array[middle]){
head = middle + 1;
}else{
end = middle - 1;
}
}
if(isFlag){
return middle;
}else{
return -1;
}
}
}
ArrayUtils.java
②在ArrayUtilsTest.java 文件中測試封裝好的方法
public class ArrayUtilsTest {
public static void main(String[] args){
ArrayUtils array = new ArrayUtils();
int[] arr = new int[]{1,5,7,9,845,21,30,12,222,56,-30};
// 獲取數(shù)組最大值
System.out.println(array.getMax(arr));
// 獲取數(shù)組最小值
System.out.println(array.getMin(arr));
// 數(shù)組反轉(zhuǎn)
array.reverseArray(arr);
for(int i = 0;i < arr.length;i++){
System.out.print(arr[i]+"\t");
}
System.out.println();
// 數(shù)組復(fù)制
int[] arr2 = new int[]{1,2,3};
int[] newArray = array.copyArray(arr2);
for(int i = 0;i < newArray.length;i++){
System.out.print(newArray[i]+"\t");
}
System.out.println();
// 查找元素
int i = array.getIndex(arr2,4);
System.out.println(i); // -1 表示沒有找到此元素
// 數(shù)組排序
int[] array3 = new int[]{1,5,8,77,98,33,0,2};
ArrayUtils a = new ArrayUtils();
System.out.print("排序前:");
a.print(array3);
System.out.println();
System.out.print("排序后:");
a.sortArray(array3);
a.print(array3);
}
}
ArrayUtilsTest.java
總結(jié)
到此這篇關(guān)于Java面向?qū)ο箢惡蛯ο蟮奈恼戮徒榻B到這了,更多相關(guān)Java面向?qū)ο箢惡蛯ο髢?nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
-
Spring 處理 HTTP 請求參數(shù)注解的操作方法
這篇文章主要介紹了Spring 處理 HTTP 請求參數(shù)注解的操作方法,本文通過實例代碼給大家介紹的非常詳細,感興趣的朋友參考下吧 2024-04-04
-
如何使用 IntelliJ IDEA 編寫 Spark 應(yīng)用程序(Sc
本教程展示了如何在IntelliJIDEA中使用Maven編寫和運行一個簡單的Spark應(yīng)用程序(例如WordCount程序),本文通過實例代碼給大家介紹的非常詳細,感興趣的朋友跟隨小編一起看看吧 2024-11-11
最新評論
1 - Java面向?qū)ο髮W(xué)習(xí)的三條主線
①Java類即類的成員:屬性 方法 構(gòu)造器 (代碼塊 內(nèi)部類)
②面向?qū)ο蟮娜筇卣鳎悍庋b性 繼承性 多態(tài)性 (抽象性)
③其他關(guān)鍵字:this supper static final abstract interface 等
2 - 面向過程與面向?qū)ο?/h2>
二者都是一種思想,面向?qū)ο笫窍鄬τ诿嫦蜻^程而言的
1-面向過程:強調(diào)的是功能行為,以函數(shù)為最小單位,考慮怎么做
2-面向?qū)ο螅簩⒐δ芊庋b進對象,強調(diào)具備了功能的對象,以類/對象為最小單位,考慮誰來做
面向?qū)ο蟾訌娬{(diào)運用人類在日常的思維邏輯中采用的思想方法與原則,如抽象,分類,繼承,聚合,多態(tài)等
/*
人把大象裝進冰箱
面向過程:強調(diào)的是功能行為,以函數(shù)為最小單位,考慮怎么做
①將冰箱門打開
②抬起大象,裝進冰箱
③關(guān)閉冰箱門
面向?qū)ο螅簩⒐δ芊庋b進對象,強調(diào)具備了功能的對象,以類/對象為最小單位,考慮誰來做
人{
打開(冰箱){
冰箱.開開門();
}
抬起(大象){
大象.進入(冰箱);
}
關(guān)閉(冰箱){
冰箱.閉合();
}
}
冰箱{
開開門(){}
閉合(){}
}
大象{
進入(冰箱){}
}
*/
3 - 面向?qū)ο蟮娜筇卣?/h2>
封裝(Encapsulation)
繼承(Inheritance)
多態(tài)(Polymorphism)
4 - 面向?qū)ο蠓治龇椒ǚ治鰡栴}的思路和步驟
/*
* 程序員從面向過程的執(zhí)行者轉(zhuǎn)化成了面向?qū)ο蟮闹笓]著
* 面向?qū)ο蠓治龇椒ǚ治鰡栴}的思路和步驟:
* 1-根據(jù)問題 需要,選擇問題所針對的現(xiàn)實世界中的實體
* 2-從實體中尋找解決問題相關(guān)的屬性和功能,這些屬性和功能就形成了概念世界中的類
* 3-把抽象的實體用計算機語言進行描述,形成計算機世界中類的定義,即借助某種程序語言,把類構(gòu)造成計算機能夠識別和處理的 數(shù)據(jù)結(jié)構(gòu)
*
* 4-將類實例化成計算機世界中的對象,對象是計算機世界中解決問題的最終工具
*/
5 - 面向?qū)ο蟮乃枷敫攀?/h2>
1-類(Class)和對象(Object)是面向?qū)ο蟮暮诵母拍睿?/p>
?、兕愂菍σ活愂挛锏拿枋?,是抽象的,概念上的定義
?、趯ο笫菍嶋H存在的該類事物的每個個體,因而也稱其為實例(instance)
?、跩ava世界里,"萬物皆對象"
2-代碼示例
/* 設(shè)計類 -> 其實就是設(shè)計類的成員 屬性 = 成員變量 = field = 域、字段 方法 = 成員方法 = 函數(shù) = method 創(chuàng)建類的對象 = 類的實例化 = 實例化類 類和對象的使用(面向?qū)ο蟮乃枷雽崿F(xiàn)) 1-創(chuàng)建類,設(shè)計類的成員 2-創(chuàng)建類的對象 3-調(diào)用對象的屬性或方法(對象.屬性 對象.方法) 如果創(chuàng)建了一個類的多個對象,則每個對象都獨立的擁有一套類的屬性。(非 static 的) 意味著,我們修改一個對象的屬性a,則不影響另一個對象的屬性a。 */ // 測試類 public class PersonTest { public static void main(String[] args){ // 創(chuàng)建Person類的對象 Person p1 = new Person(); // Scanner scanner = new Scanner(System.in); // 調(diào)用對象屬性:對象.屬性 System.out.println(p1.age); // 22 p1.name = "howie"; p1.isMale = true; // 調(diào)用對象方法:對象.方法 p1.eat(); p1.sleep(); p1.talk("chinese"); // ******************* Person p2 = new Person(); System.out.println(p2.name); // null 說明類的初始化默認值為 null Person p3 = p1; // 將p1變量保存的對象空間地址值賦值給p3,導(dǎo)致p1和p3都指向了堆空間的同一個對象實體 System.out.println(p3.name); // howie } } // 創(chuàng)建類,設(shè)計類的成員 class Person{ // 屬性 String name; int age = 22; boolean isMale; // 方法 public void eat(){ System.out.println("人正在吃飯!"); } public void sleep(){ System.out.println("人可以睡覺!"); } public void talk(String language){ System.out.println("人可以說話,使用的是"+language); } }
6 - 類和對象的創(chuàng)建-內(nèi)存解析
1-內(nèi)存解析
2-對象內(nèi)存解析
7 - 類中屬性的聲明和使用(局部變量與成員變量的區(qū)別)
1-屬性(成員變量)vs 局部變量
* 1-相同點
* ?、俣x變量的格式:數(shù)據(jù)類型 變量名 = 變量值
* ?、谙嚷暶?,后使用
* ?、圩兞慷加衅鋵?yīng)的作用域
* 2-不同點
* ?、僭陬愔新暶鞯奈恢貌煌?br />* 屬性:直接定義在類的一對{}內(nèi)
* 局部變量:聲明在方法內(nèi),方法形參,代碼塊,構(gòu)造器形參,構(gòu)造器內(nèi)部的變量
* ②關(guān)于權(quán)限修飾符的不同
* 屬性:可以在聲明屬性時,指定其權(quán)限,使用權(quán)限修飾符。常用的權(quán)限修飾符:private , public , 缺省 , protected
* 目前,大家聲明屬性時,都使用缺省就可以(什么都不加)
* 局部變量:不可以使用權(quán)限修飾符
* ③默認初始化的值
* 屬性:類的屬性,根據(jù)其類型,都有默認初始值。
* 整型(byte、short、int、long):0
* 浮點型(float、double):0.0
* 字符型(char):0(或'\u00000')
* 布爾(boolean):false
*
* 引用數(shù)據(jù)類型(類、數(shù)組、接口):null
* 局部變量:沒有默認初始化值,意味著我們在調(diào)用局部變量之前,一定要顯示賦值,特別的,形參調(diào)用時賦值即可。
* ?、茉趦?nèi)存中加載的位置
* 屬性:堆空間(非static)
* 局部變量:棧
public class test1 { public static void main(String[] args) { User u1 = new User(); System.out.println(u1.name); System.out.println(u1.age); System.out.println(u1.isMale); // 調(diào)用形參 u1.talk("English"); } } class User{ // 屬性(或成員變量) String name; int age; boolean isMale; public void talk(String language) {// 形參,也是局部變量 System.out.println("我們使用"+language+"交流"); } public void eat() { String foot = "米線"; // 在方法中定義的局部變量 System.out.println("云南人喜歡吃:"+foot); } }
⑤內(nèi)存解析需要注意兩點
1-內(nèi)存結(jié)構(gòu):棧(局部變量)、堆[new 出來的結(jié)構(gòu):對象(非 static成員變量)、數(shù)組]
2-變量:成員變量 vs 局部變量(方法內(nèi)、方法形參、構(gòu)造器內(nèi)、構(gòu)造器形參、代碼塊內(nèi))
8 - 類中方法的聲明和使用
類中方法的申明和使用
方法:描述類應(yīng)該具備的功能
比如 Math類:aqrt()\random()...
Scanner類:nexInt() ...
Arrays類:sort() \ binarySearch() \ toString() \ equals() \ ...
1-舉例
public void eat() {} public void sleep(int hour) {} public String getName() {} public String getNation(String nation) {}
2-方法的聲明
權(quán)限修飾符 返回值類型 方法名(形參列表) {
方法體
}
注意:static final abstract 來修飾的方法,后面在介紹
3-說明
?、贆?quán)限修飾符:Java規(guī)定的4種權(quán)限修飾符:private(私有的) , public , 缺省 , protected
默認方法的權(quán)限修飾符先都使用public
②返回類型:有返回值 vs 沒有返回值
1-如果方法有返回值,則必須在方法聲明時,指定返回值的類型。同時,方法中,需要使用時return關(guān)鍵字來返回指定類型的變量或常量
2-如果方法沒有返回值,則方法聲明時,使用 void來表示。通常沒有返回值的方法中,就不需要使用return關(guān)鍵字了
?、鄯椒簩儆跇俗R符,遵循標識符的規(guī)則和規(guī)范,見名知意
?、苄螀⒘斜恚悍椒梢月暶?個,1個,或多個形參...格式:數(shù)據(jù)類型1 形參1,數(shù)據(jù)類型2 形參2,...
⑤方法體:方法功能的體現(xiàn)
4-return關(guān)鍵字的使用
?、偈褂梅秶菏褂迷诜椒w中
?、谧饔茫?.結(jié)束方法2.針對于有返回值類型的方法,使用 'return 數(shù)據(jù)'方法返回所要的數(shù)據(jù)。
?、圩⒁恻c:return關(guān)鍵字后面不可以聲明執(zhí)行語句
5-方法的使用:可以調(diào)用當前類的屬性或方法
特殊的:方法A中調(diào)用方法A稱作 遞歸方法
方法中不可以再定義方法
public class CustomerTest { public static void main(String[] args) { // TODO Auto-generated method stub } } // 客戶類 class Customer{ // 屬性 String name; int age; boolean isMale; // 方法 public void eat() { System.out.println("客戶吃飯"); } public void sleep(int hour) { System.out.println("客戶休息了"+hour+"個小時"); } public String getName() { return name; } public String getNation(String nation) { String info = "我的國籍是"+nation; return info; } }
練習(xí)題
/* * 1-編寫程序,聲明一個method方法,在方法中打印一個10*8的*型矩形,在main方法中調(diào)用該方法。 * 2-修改上一個程序,在method方法中,除打印一個10*8的*矩形外形,再計算該矩形的面積,并將其作為方法返回值。 * 在main方法中調(diào)用該方法接受返回的面積值打印。 * * 3-修改上一個程序,在method方法提供m和n兩個參數(shù),方法中打印一個m*n的*型矩形 */ public class Exer3Test { public static void main(String[] args) { Exer3Test test = new Exer3Test(); // 第1題的測試 // test.method(); // 第2題的測試 // double s = test.method(); // System.out.println("面積為"+s); // 第3題的測試 System.out.println(test.method(12,10)); } // 第1問 // public void method(){ // for(int i = 0;i < 10;i++){ // for(int j = 0;j < 8;j++){ // System.out.print("* "); // } // System.out.println(); // } // } // 第2問 // public double method(){ // for(int i = 0;i < 10;i++){ // for(int j = 0;j < 8;j++){ // System.out.print("* "); // } // System.out.println(); // } // return 10*8; // 第3問 public int method(int n,int m){ for(int i = 0;i < n;i++){ for(int j = 0;j < m;j++){ System.out.print("* "); } System.out.println(); } return n * m; } }
9 - 對象數(shù)組的使用
/* * 對象數(shù)組題目 * 定義類Student,包含三個屬性:學(xué)號number(int),年級state(int),成績score(int)。創(chuàng)建20個學(xué)生對象,學(xué)號1到20 * 年級[1-6]和成績[0-100]都由隨機數(shù)確定。 * 問題1:打印出3年級(state值為3)的學(xué)生信息 * 問題2:使用冒泡排序按學(xué)生成績排序,并遍歷所有學(xué)生信息 * * 提示: * ①生成隨機數(shù):Math.random(),返回值類型double * ②四舍五入:Math.round(double d),返回值類型long. */ public class StudentTest { public static void main(String[] args){ // 聲明Student對象數(shù)組 Student[] students = new Student[20]; for(int i = 0;i < students.length;i++){ // 給數(shù)組元素賦值 students[i] = new Student(); // 給Student對象的屬性賦值 students[i].number = i + 1; double value = Math.random(); students[i].state = (int)(value * (6 - 1 + 1) + 1); double v = Math.random(); students[i].score = (int)(v * (100 + 1) + 0); // System.out.print(students[i].score+"\t"); } // 遍歷學(xué)生數(shù)組 for(int i = 0;i < students.length;i++){ // System.out.println(students[i].number+","+students[i].state+","+students[i].score); // System.out.println(students[i].showInfo()); // 問題1:打印出3年級(state值為3)的學(xué)生信息 if(students[i].state == 3){ // System.out.println(students[i].showInfo()); } } // 問題2:使用 [冒泡排序] 按學(xué)生成績排序,并遍歷所有學(xué)生信息 for(int i = 0;i < students.length - 1;i++){ for(int j = 0;j < students.length - 1 - i;j++){ if(students[j].score > students[j+1].score){ Student temp = students[j]; students[j] = students[j+1]; students[j+1] = temp; } } } // 遍歷 排好序的 學(xué)生信息 從最高分打印 for(int i = 0;i < students.length;i++){ System.out.println(students[students.length - i-1].showInfo()); } } } class Student{ int number; // 學(xué)號 int state; // 年級 int score; // 成績 public String showInfo(){ return "學(xué)號:"+number+" "+"年級:"+state+" "+"成績:"+score; } } 對象數(shù)組使用示例
數(shù)組對象內(nèi)存解析
10 - 理解 "萬物皆對象"
/*
* 1-在Java語言范疇中,我們都將功能,結(jié)構(gòu)等封裝到類中,通過類的實例化,來調(diào)用具體的功能結(jié)構(gòu)
* >Scanner,String
* >文件:File
* >網(wǎng)絡(luò)資源:URL
* 2-涉及到Java語言與前端HTML、后端的數(shù)據(jù)庫交互時、前后端的結(jié)構(gòu)在Java層面交互時,都體現(xiàn)為類、對象
*/
11 - 匿名對象
/* * 匿名對象的使用 * 1-理解:我們創(chuàng)建的對象,沒有顯示的賦給一個變量名,即為匿名對象 * 2-特征:匿名對象只能調(diào)用一次 * 3-開發(fā)中使用:以下代碼示例 */ public class PhoneTest { public static void main(String[] args) { // TODO Auto-generated method stub Phone p = new Phone(); // p = null; System.out.println(p); p.sendEmail(); p.playGame(); // 匿名對象 // new Phone().sendEmail(); // new Phone().playGame(); new Phone().price = 199; new Phone().showPrice(); // 0.0 PhoneMall mall = new PhoneMall(); // mall.show(p); // 匿名對象的使用 mall.show(new Phone()); } } class PhoneMall{ public void show(Phone phone) { phone.sendEmail(); phone.playGame(); } } class Phone{ double price; // 價格 public void sendEmail() { System.out.println("發(fā)送郵件"); } public void playGame() { System.out.println("打游戲"); } public void showPrice() { System.out.println("手機價格為"+price); } } 匿名對象的使用
12 - 方法的封裝(數(shù)組常用算法的封裝)
注意兩個Java文件在同級目錄
①在ArrayUtils.java 文件中 創(chuàng)建類并分裝方法
public class ArrayUtils { // 求數(shù)組最大值 public int getMax(int[] array){ int max = array[0]; for(int i = 1;i < array.length;i++){ if(array[i] > max){ max = array[i]; } } return max; } // 求數(shù)組最小值 public int getMin(int[] array){ int min = array[0]; for(int i = 1;i < array.length;i++){ if(min > array[i]){ min = array[i]; } } return min; } // 求數(shù)組總和 public int getSum(int[] array){ int sum = 0; for(int i = 0;i < array.length;i++){ sum += array[i]; } return sum; } // 求數(shù)組平均值 public double getAvg(int[] array){ return getSum(array) / array.length; } // 反轉(zhuǎn)數(shù)組 public void reverseArray(int[] array){ for(int i = 0;i < array.length / 2;i++){ int temp = array[i]; array[i] = array[array.length - i - 1]; array[array.length - i - 1] = temp; } } // 復(fù)制數(shù)組 public int[] copyArray(int[] array){ int[] newArray = new int[array.length]; for(int i = 0;i < array.length;i++){ newArray[i] = array[i]; } return newArray; } // 數(shù)組排序 public void sortArray(int[] array){ /** * 默認從小到大排 */ for(int i = 0;i < array.length - 1;i++){ for(int j = 0;j < array.length - i -1;j++){ if(array[j] > array[j+1]){ int temp = array[j]; array[j] = array[j+1]; array[j+1] = temp; } } } } // 遍歷數(shù)組 public void print(int[] array){ for(int i = 0;i < array.length;i++){ System.out.print(array[i]+"\t"); } } // 查找數(shù)組指定的元素 public int getIndex(int[] array,int dest){ int head = 0; int end = array.length - 1; // 初始的末索引 boolean isFlag = false; int middle = 0; sortArray(array); while(head <= end){ middle = (head + end) / 2; if(dest == array[middle]){ isFlag = true; break; }else if(dest > array[middle]){ head = middle + 1; }else{ end = middle - 1; } } if(isFlag){ return middle; }else{ return -1; } } } ArrayUtils.java
②在ArrayUtilsTest.java 文件中測試封裝好的方法
public class ArrayUtilsTest { public static void main(String[] args){ ArrayUtils array = new ArrayUtils(); int[] arr = new int[]{1,5,7,9,845,21,30,12,222,56,-30}; // 獲取數(shù)組最大值 System.out.println(array.getMax(arr)); // 獲取數(shù)組最小值 System.out.println(array.getMin(arr)); // 數(shù)組反轉(zhuǎn) array.reverseArray(arr); for(int i = 0;i < arr.length;i++){ System.out.print(arr[i]+"\t"); } System.out.println(); // 數(shù)組復(fù)制 int[] arr2 = new int[]{1,2,3}; int[] newArray = array.copyArray(arr2); for(int i = 0;i < newArray.length;i++){ System.out.print(newArray[i]+"\t"); } System.out.println(); // 查找元素 int i = array.getIndex(arr2,4); System.out.println(i); // -1 表示沒有找到此元素 // 數(shù)組排序 int[] array3 = new int[]{1,5,8,77,98,33,0,2}; ArrayUtils a = new ArrayUtils(); System.out.print("排序前:"); a.print(array3); System.out.println(); System.out.print("排序后:"); a.sortArray(array3); a.print(array3); } } ArrayUtilsTest.java
總結(jié)
到此這篇關(guān)于Java面向?qū)ο箢惡蛯ο蟮奈恼戮徒榻B到這了,更多相關(guān)Java面向?qū)ο箢惡蛯ο髢?nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring 處理 HTTP 請求參數(shù)注解的操作方法
這篇文章主要介紹了Spring 處理 HTTP 請求參數(shù)注解的操作方法,本文通過實例代碼給大家介紹的非常詳細,感興趣的朋友參考下吧2024-04-04如何使用 IntelliJ IDEA 編寫 Spark 應(yīng)用程序(Sc
本教程展示了如何在IntelliJIDEA中使用Maven編寫和運行一個簡單的Spark應(yīng)用程序(例如WordCount程序),本文通過實例代碼給大家介紹的非常詳細,感興趣的朋友跟隨小編一起看看吧2024-11-11