一.封裝的簡單理解
將類的某些信息隱藏在類的內部,不允許外部程序直接訪問,並通過該類提供的方法來實現對隱藏信息的操作和訪問。(簡單的說就是隱藏對象的信息,留出訪問的接口)。
特點:1.只能通過規定的方法訪問數據;2.隱藏類的實例細節,方便修改和實現。
二.封裝的代碼實現
注:只有getxxx的屬性是只讀屬性,只有setxxx的屬性是只寫屬性
例:

1 package com.imooc.animals; 2 3 public class Cat { 4 //成員屬性:昵稱,年齡,體重,品種 5 //修改屬性的可見性 --private 限定只能在當前類內訪問 6 private String name; 7 int month; 8 double weight; 9 String species; 10 //創建get/set方法 11 //在set和get中添加對屬性的限制 12 public void setName(String name){ 13 this.name=name; 14 } 15 public String getName(){ 16 return "My name is "+this.name; 17 } 18 public Cat(){ 19 20 } 21 //有參構造 22 public Cat(String name,int month,double weight,String species){ 23 this.name=name; 24 this.month=month; 25 this.weight=weight; 26 this.species=species; 27 28 } 29 //成員方法 30 public void eat(){ 31 System.out.println("我吃東西了"); 32 } 33 public void run(){ 34 System.out.println("我跑了"); 35 } 36 }

1 package com.imooc.animals; 2 3 public class Cattest { 4 5 public static void main(String[] args) { 6 // 對Cat實例化 7 Cat cat=new Cat(); 8 cat.setName("哈哈"); 9 String name=cat.getName(); 10 cat.month=12; 11 System.out.println(name); 12 System.out.println(cat.month); 13 System.out.println(cat.species); 14 15 16 } 17 18 }
注:Python中使用(__屬性定義私有屬性【規范】,可以自己實現方法封裝,@property屬性處理)
三.使用包進行類管理
Java同一個包下不允許存在同名類(域名倒序+模塊+功能)(如com.swpu.chains),域名全部小寫,必須放在Java源文件的第一行,建議每個包內存儲信息功能單一,一個源文件中只能有一個定義包的語句。
注:建議采用“import 包名.類名”的方式加載,提高效率;
加載類的順序跟import導入語句的位置無關【如果有兩個包中有同名的類都導入(import com.animals.Cat;import com.animals2.*會優先調用更詳細的包路徑,與導入的先后位置無關)】;
“import 包名.*”只能訪問指定包名下的類,無法訪問子包下的類
Java常用系統包:
四.static關鍵字
1.static
static修飾的成員:靜態成員,類成員,無論實例化多少個實例對象,都共享同一塊內存空間(和Python的類變量,類方法一致),類對象共享,類加載時產生,銷毀時釋放,生命周期長。【既可以使用對象名訪問,也可以使用類名訪問{推薦類名}】
static+屬性-》靜態屬性,類屬性;static+方法-》類方法,靜態方法【沒有靜態類(static+類),也沒有(static+方法中的局部變量】
注:
在成員方法中可以直接訪問呢靜態成員;
靜態方法中不能直接訪問同一個類中的非靜態成員【只能通過對象實例化后,對象.成員的方式訪問】,只能直接訪問同一個類中的靜態成員。
2.代碼塊:(每一個都是一個作用空間)
注:同一作用范圍不可以定義兩個同名的變量,但在不同的代碼塊中用多個{}相當於多個作用空間,就可以定義相同的變量,作用周期僅僅限制於自己的作用空間。
最外層的{}作用區域包含了代碼塊1和代碼塊2
普通代碼塊(在方法中的{}):
可以有多個,順序執行,先出現先執行

1 public void run(){ 2 { 3 System.out.println("我第一"); 4 } 5 System.out.println("我第二"); 6 { 7 System.out.println("我第三"); 8 } 9 }
構造代碼塊:(類中{})
創建對象時調用,優先於構造方法執行(可以有多個,順序執行),實例化一個對象就執行一次,可以對成員屬性,靜態屬性賦值。
靜態代碼塊(也是類中static{}):
類加載時調用,優先於構造代碼塊執行(可以有多個,順序執行),無論實例化多少個對象,它都只執行一次,在同一個類中只能直接給靜態成員賦值(若要給成員屬性賦值,需要實例化對象然后賦值)
五.Java封裝綜合實例
1.學生類:

1 package com.swpu.model; 2 3 public class Student { 4 // 成員屬性 學號,姓名,性別,年齡 5 private String studentNum; 6 private String studentName; 7 private String studentSex; 8 private int studentAge; 9 // 方案三,對象屬性,在沒有賦初值前默認為null(關聯強度強),傳遞的是對象的引用 10 private Subject studentSubject; 11 12 // 無參構造 13 public Student() { 14 15 } 16 17 // 有參構造 18 public Student(String studentNum, String studentName, String studentSex, int studentAge) { 19 this.setStudentNum(studentNum); 20 this.setStudentName(studentName); 21 this.setStudentSex(studentSex); 22 this.setStudentAge(studentAge); 23 } 24 25 // 方案三構造方法 26 public Student(String studentNum, String studentName, String studentSex, int studentAge, Subject subject) { 27 this.setStudentNum(studentNum); 28 this.setStudentName(studentName); 29 this.setStudentSex(studentSex); 30 this.setStudentAge(studentAge); 31 this.setStudentSubject(subject); 32 } 33 34 public void setStudentNum(String studentNum) { 35 this.studentNum = studentNum; 36 } 37 38 public String getStudentNum() { 39 return this.studentNum; 40 } 41 42 public String getStudentName() { 43 return studentName; 44 } 45 46 public void setStudentName(String studentName) { 47 this.studentName = studentName; 48 } 49 50 public String getStudentSex() { 51 return studentSex; 52 } 53 54 public void setStudentSex(String studentSex) { 55 if (studentSex == "男" || studentSex == "女") { 56 this.studentSex = studentSex; 57 } else { 58 this.studentSex = "男"; 59 } 60 } 61 62 public int getStudentAge() { 63 return studentAge; 64 } 65 /** 66 * 專業返回 如果還未實例化(默認為null),實例化后返回 67 * @return專業對象 68 */ 69 public Subject getStudentSubject() { 70 71 if(this.studentSubject==null){ 72 Subject studentSubject=new Subject(); 73 } 74 return studentSubject; 75 } 76 77 public void setStudentSubject(Subject studentSubject) { 78 this.studentSubject = studentSubject; 79 } 80 81 /** 82 * 年齡必須z在10到00之間,否則為18 83 * 84 * @param studentAge 85 */ 86 public void setStudentAge(int studentAge) { 87 if (studentAge < 10 || studentAge > 100) { 88 this.studentAge = 18; 89 } else { 90 this.studentAge = studentAge; 91 } 92 } 93 94 /** 95 * 學生自我介紹方法 96 * 97 * @return 學號,姓名,性別,年齡 98 */ 99 public String introduction() { 100 String str = "學生信息如下:\n" + "學生學號:" + this.getStudentNum() + "\n學生姓名:" + this.getStudentName() + "\n學生性別:" 101 + this.getStudentSex() + "\n學生年齡:" + this.getStudentAge()+"\n專業名稱:"+this.getStudentSubject().getSubjectName()+"\n專業年制:"+this.getStudentSubject().getSubjectLife(); 102 return str; 103 104 } 105 106 // 連接專業信息和學生 107 // 方案一:在函數中添加兩個參數表示專業和學制 108 //優點:容易理解;缺點:如果需要的信息比較多,參數列表過長,組織麻煩 109 /** 110 * 學生自我介紹方法重載 111 * 112 * @param subjectName,subjectLife 113 * @param subjectLife 114 * @return 學號,姓名,性別,年齡,專業,年限 115 */ 116 public String introduction(String subjectName, int subjectLife) { 117 String str = "學生信息如下:\n" + "學生學號:" + this.getStudentNum() + "\n學生姓名:" + this.getStudentName() + "\n學生性別:" 118 + this.getStudentSex() + "\n學生年齡:" + this.getStudentAge() + "\n所選專業為:" + subjectName + "\n專業年限為:" 119 + subjectLife; 120 return str; 121 122 } 123 124 // 方案二:在方法中添加一個專業對象作為參數,通過對象屬性獲取相應信息 125 // 相比更好:可以獲取對象的所有屬性 126 //優點:獲取方便;缺點:關聯強度不如方法三 127 /** 128 * 學生自我介紹方法重載 129 * 130 * @param 專業對象 131 * @param subjectLife 132 * @return 學號,姓名,性別,年齡,專業,年限 133 */ 134 public String introduction(Subject mysubject) { 135 String str = "學生信息如下:\n" + "學生學號:" + this.getStudentNum() + "\n學生姓名:" + this.getStudentName() + "\n學生性別:" 136 + this.getStudentSex() + "\n學生年齡:" + this.getStudentAge() + "\n所選專業為:" + mysubject.getSubjectName() 137 + "\n專業年限為:" + mysubject.getSubjectLife(); 138 return str; 139 140 } 141 // 方案三:添加專業對象作為屬性,通過屬性獲取相關信息 142 }
2.專業類:

1 package com.swpu.test; 2 3 import com.swpu.model.Student; 4 import com.swpu.model.Subject; 5 6 public class SchoolTest { 7 public static void main(String[] args) { 8 // 測試subject 9 Subject subject = new Subject("軟件工程", "J10022", 4); 10 System.out.println(subject.info()); 11 System.out.println("**********************************"); 12 // 學生類測試 13 //Student stu1 = new Student("201631062222", "LYQ1", "男", 20); 14 //System.out.println(stu1.introduction()); 15 // 帶參學生測試(傳入專業和年制) 16 System.out.println("**********************************"); 17 Student stu2 = new Student("201631062232", "LYQ2", "男", 20); 18 System.out.println(stu2.introduction("軟件工程", 4)); 19 // 帶參學生測試(傳入專業對象) 20 System.out.println("**********************************"); 21 Student stu3 = new Student("201631062242", "LYQ3", "女", 20); 22 System.out.println(stu3.introduction(subject)); 23 //將專業對象作為屬性 24 System.out.println("**********************************"); 25 Student stu4 = new Student("201631062252", "LYQ4", "女", 18,subject); 26 System.out.println(stu4.introduction()); 27 //指定專業到底有多少人報名 28 subject.addStudent(stu2); 29 subject.addStudent(stu3); 30 subject.addStudent(stu4); 31 System.out.println("專業為"+subject.getSubjectName()+"已有"+subject.getStudentNum()+"人數報名"); 32 33 } 34 35 }
3.測試類:

package com.swpu.test; import com.swpu.model.Student; import com.swpu.model.Subject; public class SchoolTest { public static void main(String[] args) { // 測試subject Subject subject = new Subject("軟件工程", "J10022", 4); System.out.println(subject.info()); System.out.println("**********************************"); // 學生類測試 //Student stu1 = new Student("201631062222", "LYQ1", "男", 20); //System.out.println(stu1.introduction()); // 帶參學生測試(傳入專業和年制) System.out.println("**********************************"); Student stu2 = new Student("201631062232", "LYQ2", "男", 20); System.out.println(stu2.introduction("軟件工程", 4)); // 帶參學生測試(傳入專業對象) System.out.println("**********************************"); Student stu3 = new Student("201631062242", "LYQ3", "女", 20); System.out.println(stu3.introduction(subject)); //將專業對象作為屬性 System.out.println("**********************************"); Student stu4 = new Student("201631062252", "LYQ4", "女", 18,subject); System.out.println(stu4.introduction()); //指定專業到底有多少人報名 subject.addStudent(stu2); subject.addStudent(stu3); subject.addStudent(stu4); System.out.println("專業為"+subject.getSubjectName()+"已有"+subject.getStudentNum()+"人數報名"); } }