第15天学习Java的笔记(封装性,this,构造方法)
生活随笔
收集整理的這篇文章主要介紹了
第15天学习Java的笔记(封装性,this,构造方法)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
還有35天!
面向?qū)ο笕筇卣髦庋b性
package Demo1501;/** 面向?qū)ο笕筇卣?#xff1a;封裝、繼承、多態(tài)** 封裝性在Java中的體現(xiàn):* 1.方法就是一種封裝* 2.關(guān)鍵字private也是一種封裝** 封裝就是將一些細節(jié)信息隱藏起來,對于外界不可見* */ public class Demo01Method {public static void main(String[] args) {int[] array = {10, 15, 20, 25, 21};/*int max = array[0];for (int i = 1; i < array.length; i++) {if (max < array[i]) {max = array[i];}}*/System.out.println("最大值是:" + maxArray(array));}public static int maxArray(int[] array) {int max = array[0];for (int i = 1; i < array.length; i++) {if (max < array[i]) {max = array[i];}}return max;} }private關(guān)鍵字的作用和使用
package Demo1501;/* * 問題描述:定義person年齡時,無法阻止不合理的數(shù)值被設(shè)置進來(負數(shù))。 * 解決方案:用到private,關(guān)鍵字將需要保護的成員變量進行修飾。 * * 一旦使用private進行修飾,那么本類當(dāng)中仍然可以隨意訪問。 * 但是!超出了本類范圍之外就不能再直接訪問了。 * * 間接訪問private成員變量,就定義一對兒Getter/Setter方法 * 在間接訪問的過程中,可以在小括號內(nèi)部對其進行設(shè)置,提高了代碼的安全性 * * 必須叫setXxx或者是getXxx命名規(guī)則。 * 對于Getter來說,不能有參數(shù),返回值類型和成員變量對應(yīng)。 * 對于Setter來說,不能有返回值,參數(shù)類型和成員變量對應(yīng)。 * */public class Person {String name;private int age;public void show() {System.out.println("我叫" + name +",今年" + age + "了。");}//這個成員方法,專門用于向age設(shè)置數(shù)據(jù)public void setAge(int num) {if (num >= 0){age = num;}else {System.out.println("年齡不合法");}}//這個成員方法,專門用于獲取age的數(shù)據(jù)public int getAge(){return age;} } package Demo1501;public class Demo02Person {public static void main(String[] args) {Person person = new Person();person.name = "hh";//person.age = -18;//直接訪問private內(nèi)容是錯誤的person.setAge(20);person.show();} }練習(xí)使用private關(guān)鍵字創(chuàng)建學(xué)生類
package Demo1501;/* * 對于基本類型當(dāng)中的boolean值,Getter方法一定要寫成isXxx的形式,而setXxx規(guī)則不變 * */public class Student {private String name;private int age;private boolean male;//是不是男的public void setName(String str) {name = str;}public String getName(){return name;}public void setAge(int num) {age = num;}public int getAge(){return age;}public void setMale(boolean b){male = b;}//布爾類型的get應(yīng)該叫isXxxpublic boolean isMale(){return male;} } package Demo1501;public class Demo04Student {public static void main(String[] args) {Student stu = new Student();stu.setName("hh");//System.out.println(stu.getName());stu.setAge(30);//System.out.println(stu.getAge());stu.setMale(true);//System.out.println(stu.isMale());System.out.println("姓名是" + stu.getName() + "的人,性別為" + stu.isMale() + "今年" + stu.getAge());} }this關(guān)鍵字的作用
package Demo1502;/* * 當(dāng)方法的局部變量和類的成員變量重名的時候,根據(jù)就近原則,優(yōu)先使用局部變量。 * 如果需要訪問本類中的成員變量,需要使用格式:this.成員變量名 * 可以解決重名區(qū)分問題 * “通過誰調(diào)用的方法,值就是this” * */public class Person {String name;//我自己的名字//參數(shù)who是對方的名字//成員變量name是自己的名字public void sayHello(String name) {System.out.println(name + "你好,我是" + this.name);} } package Demo1502;public class Demo01Person {public static void main(String[] args) {Person person = new Person();//設(shè)置我自己的名字person.name = "xx";person.sayHello("hh");} }構(gòu)造方法
package Demo1502;/** 構(gòu)造方法是專門用來創(chuàng)建對象的方法,當(dāng)我們通過關(guān)鍵字new來創(chuàng)建對象時,其實就是在調(diào)用構(gòu)造方法* 格式:* public 類名稱(參數(shù)類型 參數(shù)名稱) {* 方法體* }** 注意事項:* 1.構(gòu)造方法的名稱必須和所在的類名稱完全一樣,就連大小寫也要一樣* 2.構(gòu)造方法不要寫返回值類型,連void都不用寫* 3.構(gòu)造方法不能return一個具體的返回值* 4.如果沒有編寫任何構(gòu)造方法,那么編譯器將會默認贈送一個構(gòu)造方法,沒有參數(shù),方法體什么都不做* public Student(){}* 5.一旦編寫了至少一個構(gòu)造方法,那么編譯器將不再贈送。* 6.構(gòu)造方法也是可以進行重載的。* 重載:方法名稱相同,參數(shù)列表不同。* */ public class Student {//成員變量private String name;private int age;//無參數(shù)的構(gòu)造方法public Student() {System.out.println("無參構(gòu)造方法執(zhí)行");}//有參數(shù)的構(gòu)造方法public Student(String name, int age) {this.name = name;this.age = age;System.out.println("全參構(gòu)造方法執(zhí)行");}//Getter和Setterpublic void setName(String name) {this.name = name;}public String getName() {return name;}public void setAge(int age) {this.age = age;}public int getAge() {return age;} } package Demo1502;public class Demo02Student {public static void main(String[] args) {Student stu1 = new Student();//相當(dāng)于執(zhí)行了無參構(gòu)造方法Student stu2 = new Student("hh",23);//相當(dāng)于執(zhí)行全參構(gòu)造方法System.out.println(stu2.getName() + ";" +stu2.getAge());//如果需要改變對象當(dāng)中的成員變量數(shù)據(jù)內(nèi)容,仍然還需要使用setXxx方法stu2.setAge(24);//改變年齡System.out.println(stu2.getName() + ";" + stu2.getAge());} }總結(jié)
以上是生活随笔為你收集整理的第15天学习Java的笔记(封装性,this,构造方法)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 第14天学习Java的笔记(成员变量和局
- 下一篇: 第16天学习Java的笔记(标准类,Sc