国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

黑馬程序員_JavaSE學(xué)習(xí)總結(jié)第07天_面向?qū)ο?

2019-11-15 00:08:47
字體:
供稿:網(wǎng)友
黑馬程序員_javaSE學(xué)習(xí)總結(jié)第07天_面向?qū)ο?

------- android培訓(xùn)、java培訓(xùn)、期待與您交流! ----------

07.01 成員變量和局部變量的區(qū)別

1.在類中的位置不同

成員變量 類中方法外

局部變量 方法內(nèi)或者方法聲明上

2.在內(nèi)存中的位置不同

成員變量 堆內(nèi)存

局部變量 棧內(nèi)存

3.生命周期不同

成員變量 隨著對象的存在而存在,隨著對象的消失而消失

局部變量 隨著方法的調(diào)用而存在,隨著方法的調(diào)用完畢而消失

4.初始化值不同

成員變量 有默認(rèn)的初始化值

局部變量 沒有默認(rèn)的初始化值,必須先定義,賦值,才能使用。

07.02 方法的形式參數(shù)是類名的調(diào)用

例:

 1 class Demo 2 { 3     public static void main(String[] args) 4     { 5         //創(chuàng)建學(xué)生對象 6         Student s = new Student(); 7         StudentDemo sd = new StudentDemo(); 8         sd.method(s); 9     }10 }11 class StudentDemo12 {13     //形式參數(shù)是引用類型14     public void method(Student s)15     {16         s.show();17     }18 }19 class Student20 {21     public void show()22     {23         System.out.

07.03 匿名對象的概述和應(yīng)用

匿名對象:就是沒有名字的對象。是對象的一種簡化表示形式

匿名對象的兩種使用情況:

1.對象調(diào)用方法僅僅一次的時候

2.作為實(shí)際參數(shù)傳遞

例:

 1 class Demo 2 { 3     public static void main(String[] args) 4     { 5         //匿名對象調(diào)用方法 6         new Student().show(); 7     } 8 } 9 class Student10 {11     public void show()12     {13         System.out.println("學(xué)生愛學(xué)習(xí)");14     }15 }

匿名對象調(diào)用完畢就是垃圾,垃圾回收器會在適當(dāng)?shù)臅r間回收,提高內(nèi)存的使用效率

07.04 封裝的概述

封裝概述:是指隱藏對象的屬性和實(shí)現(xiàn)細(xì)節(jié),僅對外提供公共訪問方式。

例:

 1 class Student 2 { 3     String name; 4     //將age私有,只能在Student類中訪問 5     private int age;  6  7     //提供對外訪問的方法獲取age的值 8     public int getAge() 9     {10         return age;11     }12     //提供對外訪問的方法,并可以對傳入的a的值進(jìn)行判斷,滿足條件則賦值給age,不滿足則不賦值13     public void setAge(int a)14     {15         age = a;16     }17     public void show()18     {19         System.out.println("姓名:"+name+"   "+"年齡:"+age);20     }21 }

07.05 封裝的好處和設(shè)計原則

封裝好處:

1.隱藏實(shí)現(xiàn)細(xì)節(jié),提供公共的訪問方式

2.提高了代碼的復(fù)用性

3.提高安全性

封裝原則:將不需要對外提供的內(nèi)容都隱藏起來。把屬性隱藏,提供公共方法對其訪問。

07.06 private關(guān)鍵字的概述和特點(diǎn)

private關(guān)鍵字:是一個權(quán)限修飾符。可以修飾成員(成員變量和成員方法),被private修飾的成員只在本類中才能訪問。

private最常見的應(yīng)用:

1.把成員變量用private修飾

2.提供對應(yīng)的getXxx()/setXxx()方法

07.07 private的應(yīng)用標(biāo)準(zhǔn)案例

 1 class Demo 2 { 3     public static void main(String[] args) 4     { 5         Student s = new Student(); 6         //給成員變量賦值 7         s.setName("小強(qiáng)"); 8         s.setAge(25); 9 10         System.out.println(s.getName()+":"+s.getAge());11     }12 }13 class Student14 {15     private String name;16     //將age私有,只能在Student類中訪問17     private int age; 18 19     //提供對外訪問的方法獲取name的值20     public String getName()21     {22         return name;23     }24     //提供對外訪問的方法設(shè)置name的值25     public void setName(String n)26     {27         name = n;28     }29     //提供對外訪問的方法獲取age的值30     public int getAge()31     {32         return age;33     }34     //提供對外訪問的方法設(shè)置age的值35     public void setAge(int a)36     {37         age = a;38     }39 }

07.08 this關(guān)鍵字的概述和應(yīng)用

this:代表所在類的對象引用

方法被哪個對象調(diào)用,this就代表那個對象

當(dāng)局部變量隱藏成員變量時使用this

例:

 1 class Student 2 { 3     private String name; 4     //將age私有,只能在Student類中訪問 5     private int age;  6  7     //提供對外訪問的方法獲取name的值 8     public String getName() 9     {10         return name;11     }12     //提供對外訪問的方法設(shè)置name的值13     public void setName(String name)14     {15         this.name = name;16     }17     //提供對外訪問的方法獲取age的值18     public int getAge()19     {20         return age;21     }22     //提供對外訪問的方法設(shè)置age的值23     public void setAge(int age)24     {25         this.age = age;26     }27 }

07.09 this關(guān)鍵字的內(nèi)存圖解

07.10 標(biāo)準(zhǔn)的手機(jī)類代碼及其測試

 1 class Demo 2 { 3     public static void main(String[] args) 4     { 5         Phone p = new Phone(); 6         System.out.println("品牌:"+p.getBrand()+" 價格:"+p.getPrice()+" 顏色:"+p.getColor()); 7         p.setBrand("小米"); 8         p.setPrice(1999); 9         p.setColor("白色");10         System.out.println("品牌:"+p.getBrand()+" 價格:"+p.getPrice()+" 顏色:"+p.getColor());11     }12 }13 class Phone14 {15     private String brand;//品牌16     private int price;//價格 17     private String color;//顏色18 19     public String getBrand()20     {21         return brand;22     }23     public void setBrand(String brand)24     {25         this.brand = brand;26     }27 28     public int getPrice()29     {30         return price;31     }32     public void setPrice(int price)33     {34         this.price = price;35     }36 37     public String getColor()38     {39         return color;40     }41     public void setColor(String color)42     {43         this.color = color;44     }45 }

運(yùn)行結(jié)果:

品牌:null 價格:0 顏色:null品牌:小米價格:1999 顏色:白色

07.11 構(gòu)造方法概述和格式

構(gòu)造方法作用概述:給對象的數(shù)據(jù)進(jìn)行初始化

構(gòu)造方法格式:

1.方法名與類名相同

2.沒有返回值類型,連void都沒有

3.沒有具體的返回值

例:

07.12 構(gòu)造方法的重載及注意事項(xiàng)

構(gòu)造方法注意事項(xiàng):

1.如果不提供構(gòu)造方法,系統(tǒng)會給出默認(rèn)構(gòu)造方法

2.如果提供了構(gòu)造方法,系統(tǒng)將不再提供

3.構(gòu)造方法也是可以重載的

07.13 成員方法的分類及使用

方法具體劃分:

根據(jù)返回值:1.有明確返回值方法 2.返回void類型的方法

根據(jù)形式參數(shù):1.無參方法 2.帶參方法

例:

 1 class Demo 2 { 3     public static void main(String[] args) 4     { 5         Student s = new Student(); 6         String str = s.getString(); 7         System.out.println(str); 8         s.show(); 9         s.method("小強(qiáng)");10     }11 }12 class Student13 {14     private String name;15     private int age;16     //有明確返回值的無參方法17     public String getString()18     {19         return "hello";20     }21     //返回void類型的無參方法22     public void show()23     {24         System.out.println("show run");25     }26     //返回void類型的帶參方法27     public void method(String name)28     {29         System.out.println("name:"+name);30     }31 }

07.14 一個標(biāo)準(zhǔn)學(xué)生類的代碼及測試

類的組成:

1.成員變量

2.構(gòu)造方法[無參構(gòu)造方法 / 帶參構(gòu)造方法]

3.成員方法[getXxx() / setXxx()]

給成員變量賦值的方式

1.無參構(gòu)造方法 + setXxx()

2.帶參構(gòu)造方法

 1 /* 2 學(xué)生類: 3         成員變量:name,age 4         構(gòu)造方法:無參,帶兩個參 5         成員方法:getXxx()/setXxx() 6         show():輸出該類的所有成員變量值 7              8     給成員變量賦值: 9         A:setXxx()方法10         B:構(gòu)造方法11         12     輸出成員變量值的方式:13         A:通過getXxx()分別獲取然后拼接14         B:通過調(diào)用show()方法搞定15 */16 //測試類17 class Demo18 {19     public static void main(String[] args)20     {21         //方式1給成員變量賦值22         //無參構(gòu)造+setXxx()23         Student s1 = new Student();24         s1.setName("小明");25         s1.setAge(27);26         //輸出值27         System.out.println(s1.getName()+"---"+s1.getAge());28         System.out.println("----------------------------");29         30         //方式2給成員變量賦值31         Student s2 = new Student("小強(qiáng)",30);32         s2.show();33     }34 }35 36 class Student 37 {38     //姓名39     private String name;40     //年齡41     private int age;42     43     //構(gòu)造方法44     public Student() 45     {46     }47     48     public Student(String name,int age) 49     {50         this.name = name;51         this.age = age;52     }53     54     public String getName() 55     {56         return name;57     }58     59     public void setName(String name) 60     {61         this.name = name;62     }63     64     public int getAge() 65     {66         return age;67     }68     69     public void setAge(int age) 70     {71         this.age = age;72     }73     74     //輸出所有的成員變量值75     public void show() 76     {77         System.out.println(name+"---"+age);78     }79 }

07.15 一個標(biāo)準(zhǔn)的手機(jī)的代碼及測試

 1 //測試類 2 class Demo 3 { 4     public static void main(String[] args) 5     { 6         //創(chuàng)建對象 7         Phone p = new Phone(); 8          9         //給成員變量賦值10         p.setBrand("小米");11         p.setPrice(2299);12         p.setColor("白色");13         14         //獲取值15         System.out.println(p.getBrand()+"---"+p.getPrice()+"---"+p.getColor());16     }17 }18 19 //定義手機(jī)類20 class Phone21 {22     //品牌23     private String brand;24     //價格25     private int price;26     //顏色27     private String color;28     29     //無參構(gòu)造方法30     public Phone() 31     {32     }33     34     //getXxx()和setXxx()方法35     public String getBrand() 36     {37         return brand;38     }39     public void setBrand(String brand) 40     {41         this.brand = brand;42     }43     44     public int getPrice() 45     {46         return price;47     }48     public void setPrice(int price)49     {50         this.price = price;51     }52     53     public String getColor() 54     {55         return color;56     }57     public void setColor(String color) 58     {59         this.color = color;60     } 61 }

07.16 創(chuàng)建對象做了哪些事情

Student s = new Student();在內(nèi)存中做了哪些事情?

1.加載Student.class文件進(jìn)內(nèi)存

2.在棧內(nèi)存為s開辟空間

3.在堆內(nèi)存為學(xué)生對象開辟空間

4.對學(xué)生對象的成員變量進(jìn)行默認(rèn)初始化

5.對學(xué)生對象的成員變量進(jìn)行顯示初始化

6.通過構(gòu)造方法對學(xué)生對象的成員變量賦值

7.學(xué)生對象初始化完畢,把對象地址賦值給s變量

07.17 什么時候定義成員變量

如果這個變量是用來描述這個類的信息的,那么,該變量就應(yīng)該定義為成員變量。變量的范圍越小越好,因?yàn)槟芗皶r的被回收。

07.18 長方形案例練習(xí)

定義一個長方形類,定義求周長和面積的方法,然后定義一個測試類Demo,進(jìn)行測試。

 1 /* 2 成員變量:長,寬 3 成員方法: 4          求周長:(長+寬)*2; 5          求面積:長*寬 6 */ 7 import java.util.Scanner; 8 class ChangFangXing  9 {10     //長方形的長11     private int length;12     //長方形的寬13     private int width;14     15     public ChangFangXing()16     {17     }18     19     //僅僅提供setXxx()即可20     public void setLength(int length) 21     {22         this.length = length;23     }24     25     public void setWidth(int width) 26     {27         this.width = width;28     }29     30     //求周長31     public int getZhouChang() 32     {33         return (length + width) * 2;34     }35     36     //求面積37     public int getArea() 38     {39         return length * width;40     }41 }42 43 //測試類44 class Demo45 {46     public static void main(String[] args)47     {48         //創(chuàng)建鍵盤錄入對象49         Scanner sc = new Scanner(System.in);50         51         System.out.println("請輸入長方形的長:");52         int length = sc.nextInt();53         System.out.println("請輸入長方形的寬:");54         int width = sc.nextInt();55         56         //創(chuàng)建對象57         ChangFangXing cfx = new ChangFangXing();58         //先給成員變量賦值59         cfx.setLength(length);60         cfx.setWidth(width);61         62         System.out.println("周長是:"+cfx.getZhouChang());63         System.out.println("面積是:"+cfx.getArea());64     }65 }

07.19 員工類案例練習(xí)

 1 /* 2 成員變量:員工編號,姓名,年齡 3 構(gòu)造方法:無參構(gòu)造方法 4 成員方法: 5          getXxx()/setXxx(); 6          show(); 7 */ 8  9 class Employee 10 {11     //員工編號12     private String employeeId;13     //姓名14     private String name;15     //年齡16     private int age;17     18     //構(gòu)造方法19     public Employee() 20     {21     }22     23     //getXxx()/setXxx()24     public String getEmployeeId() 25     {26         return employeeId;27     }28     29     public void setEmployeeId(String employeeId) 30     {31         this.employeeId = employeeId;32     }33     34     public String getName() 35     {36         return name;37     }38     39     public void setName(String name) 40     {41         this.name = name;42     }43     44     public int getAge() 45     {46         return age;47     }48     49     public void setAge(int age) 50     {51         this.age = age;52     }53     54     //顯示所有成員信息的方法55     public void show() 56     {57         System.out.println("員工編號:"+employeeId+"  姓名:"+name+"  年齡:"+age);58     }59 }60 61 //測試類62 class Demo63 {64     public static void main(String[] args)65     {66         //創(chuàng)建對象67         Employee e = new Employee();68         69         //給成員變量賦值70         e.setEmployeeId("ID8899");71         e.setName("旺財");72         e.setAge(18);73         74         //獲取數(shù)據(jù)75         //System.out.println(e.getEmployeeId()+"---"+e.getName()+"---"+e.getAge());76     77         //使用show方法78         e.show();79     }80 }

07.20 實(shí)現(xiàn)加減乘除并測試

 1 import java.util.Scanner; 2 class MyMath  3 { 4     //加法功能 5     public int add(int a,int b)  6     { 7         return a + b; 8     } 9     10     //減法功能11     public int sub(int a,int b) 12     {13         return a - b;14     }15     16     //乘法功能17     public int mul(int a,int b)18     {19         return a * b;20     }21     22     //除法功能23     public int div(int a,int b) 24     {25         return a / b;26     }27 }28 29 //測試類30 class Demo31 {32     public static void main(String[] args)33     {34         //創(chuàng)建鍵盤錄入對象35         Scanner sc = new Scanner(System.in);36         37         System.out.println("請輸入第一個操作數(shù):");38         int num1 = sc.nextInt();39         System.out.println("請輸入第二個操作數(shù):");40         int num2 = sc.nextInt();41         42         //創(chuàng)建MyMath對象,并使用43         MyMath mm = new MyMath();44         45         System.out.println(num1+"+"+num2+"="+mm.add(num1,num2));46         System.out.println(num1+"-"+num2+"="+mm.sub(num1,num2));47         System.out.println(num1+"*"+num2+"="+mm.mul(num1,num2));48         System.out.println(num1+"/"+num2+"="+mm.div(num1,num2));49     }50 }

運(yùn)行結(jié)果:

請輸入第一個操作數(shù):8請輸入第二個操作數(shù):28+2=108-2=68*2=168/2=4

07.21 static關(guān)鍵字的引入

例:

 1 class Person 2 { 3     String name; 4     //使用static修飾的數(shù)據(jù)稱為對象的共享數(shù)據(jù) 5     static String country = "中國"; 6     public void show() 7     { 8         System.out.println("姓名:"+name+"  國家:"+country); 9     }10 }11 12 class Demo13 {14     public static void main(String[] args)15     {16         Person p1 = new Person();17         p1.name = "小明";18         p1.show();19 20         Person p2 = new Person();21         p2.name = "小紅";22         p2.country = "美國";23         p2.show();24         p1.show();25 26     }27 }

運(yùn)行結(jié)果:

姓名:小明  國家:中國姓名:小紅  國家:美國姓名:小明  國家:美國

07.22 static關(guān)鍵字的特點(diǎn)

1.static是一個修飾符,用于修飾成員(成員變量和成員函數(shù))

2.static修飾的成員被所有的對象共享

3.static優(yōu)先于對象存在,因?yàn)閟tatic成員隨著類的加載就已經(jīng)存在了

4.static修飾的成員可以被對象調(diào)用,也可以直接被類名調(diào)用,格式為:類名.靜態(tài)成員

5.static修飾的數(shù)據(jù)是共享數(shù)據(jù),對象中存儲的是特有數(shù)據(jù)

07.23 static的內(nèi)存圖解

07.24 static的注意事項(xiàng)

1.在靜態(tài)方法中是沒有this關(guān)鍵字的

2.靜態(tài)方法只能訪問靜態(tài)的成員變量和靜態(tài)的成員方法

3.主函數(shù)是靜態(tài)的,如果要在主函數(shù)中調(diào)用非靜態(tài)成員可以創(chuàng)建一個對象來調(diào)用

07.25 靜態(tài)變量和成員變量的區(qū)別

1.所屬不同

靜態(tài)變量屬于類,所以也稱為為類變量

成員變量屬于對象,所以也稱為實(shí)例變量(對象變量)

2.內(nèi)存中位置不同

靜態(tài)變量存儲于方法區(qū)的靜態(tài)區(qū)

成員變量存儲于堆內(nèi)存

3.內(nèi)存出現(xiàn)時間不同

靜態(tài)變量隨著類的加載而加載,隨著類的消失而消失

成員變量隨著對象的創(chuàng)建而存在,隨著對象的消失而消失

4.調(diào)用不同

靜態(tài)變量可以通過類名調(diào)用,也可以通過對象調(diào)用

成員變量只能通過對象名調(diào)用

07.26 main方法的格式詳細(xì)解釋


發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 老河口市| 正蓝旗| 临漳县| 兴海县| 桃江县| 隆林| 彩票| 武强县| 瑞昌市| 金塔县| 军事| 南宫市| 周至县| 宝鸡市| 桃江县| 绥棱县| 介休市| 个旧市| 儋州市| 阜宁县| 崇礼县| 贵港市| 乌拉特前旗| 赫章县| 松原市| 禹州市| 军事| 关岭| 宁强县| 固阳县| 大庆市| 海安县| 修文县| 丰顺县| 滕州市| 呼伦贝尔市| 芮城县| 安泽县| 芮城县| 历史| 如皋市|