??? 一、封裝:
一般意義的封裝:把一段重復代碼抽取成一個函數,稱為代碼的封裝(包裝)面向對象語言的封裝:將類的某些信息隱藏在類的內部(通過使用不同的訪問權限修飾符),不許外部程序直接訪問 而是通過該類提供的方法來實現對隱藏信息的操作和訪問封裝案例1:將成員變量私有化封裝案例2:將成員方法私有化 java固定模式:解決某種問題的固定方式(算法)單例模式:讓一個類在一個程序中只能創建一個對象(window_TestWindow)將類的構造方法私有化,外界不可以隨便調用,向外界提供一個方法去構建在類中new一個static的自己,保證在類生成時,就生成了唯一的對象補充變量的分類:1、數據類型:基本數據類型、引用數據類型2、按照位置分:成員變量:在定義類中,可以使用權限修飾符修飾;在構造方法中進行自動賦值;生命周期不同:靜態(隨著類的加載而生成,類的銷毀而銷毀)非靜態(隨著對象的創建,銷毀而創建銷毀)存儲位置:靜態:方法區非靜態:堆局部變量:在定義方法中,不能使用權限修飾符修飾必須自己進行初始化生命周期:隨著方法調用而生成,方法調用結束而結束存儲位置:棧
例:
package com.wbc.opp.oop.day3.packAge;public class Person {private String name;//私人權限private int age;//私人權限//定義公共的成員方法,可以在此類方法中加入控制語句// 外界通過調用方法來訪問成員變量public Person(){}//通過構造方法public Person(String name,int age){this.name=name;this.age=age;}public void setName(String name){if(name.length()>2&&name.length()<6){this.name=name;}}public String getName(){return name;}public void setAge(int age){if(age<150&&age>0){this.age=age;}}public int getAge(){return this.age;}
}
package com.wbc.opp.oop.day3.packAge;public class TestPerson {public static void main(String[] args) {Person p1=new Person();/*String name;在其他類中可以直接對類中的屬性進行賦值操作,但沒法控制其賦值的內容p1.name="sdadsa";*/p1.setName("abac");String p1Name= p1.getName();System.out.println(p1Name);p1.setAge(2);int p1Age=p1.getAge();System.out.println(p1Age);}
}
二、繼承
繼承就是將同一類事物中的共性進行抽取,定義在一個類中(基類) 其他類可以繼承基類,就能擁有基類中的功能,實現代碼的復用類 以及可以在子類中擴展屬于子類自己的功能而不影響其他類例:貓狗都是動物,所以可以抽取一個動物類,以繼承功能優點:提高了代碼的重復率,減少了冗余。子類使用extends關鍵字繼承父類 一個類只能繼承一個類,但可以多層繼承(繼承體系)Object是所有類的父類,是java類體系中最頂尖的類 當一個類沒有顯示繼承,默認繼承Objectdog繼承Animal,Dog稱為子類,派生類;Animal成為父類方法覆蓋:可能有些功能比較籠統或者與具體有差距,需要更加細致的功能。所以出現了方法的覆蓋當父類中方法的實現不能滿足子類需求是,可以在子類中對父類的方法進行覆蓋要求:子類重寫的方法結構與父類方法結構一致(權限大于等于父類權限;返回值,名字,參數必須一致)父類的私有方法不能重寫,挎包的默認權限方法不能重寫子類拋出的異常不能大于父類的異常 @Override//注解標簽。表示此方法是從父類重寫來的,檢查重寫是不是正確 可以不用添加,只需要重寫正確即可,但建議保留(編譯器可以進行語法驗證;增加可讀性)super關鍵字:當覆寫后子類需要調用父類的方法,可以用super關鍵字調用 例如
@Overridepublic void sleep() {super.sleep();//使用super調用父類方法System.out.println("哮天犬不需要睡覺");}
super不僅可以調用父類,還可以調用父類的父類 super與this相似,this表示當前的對象,super代表父類的內存空間的標識 this在用的時候需要創建本類對象,但super沒有創建父類對象,只是將父類的信息存入子類繼承中的構造方法:在子類繼承父類時不會繼承構造方法,只能通過super構造在子類的構造方法首行調用父類的構造方法-> super(無參調無參,有參調有參);不寫會默認無參構造(super();)常見錯誤:默認無參構造時,父類需要有無參構造方法
例:
Animal是父類
package com.wbc.opp.oop.day3.inherit;public class Animal {private String breed;//品種private String name;private int age;private String gender;public String getBreed() {return breed;}public void setBreed(String breed) {this.breed = breed;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public String getGender() {return gender;}public void setGender(String gender) {this.gender = gender;}public void printInfo(){System.out.println("姓名:"+name);System.out.println("品種:"+breed);System.out.println("性別:"+gender);System.out.println("年齡"+age);}public Animal() {}public Animal(String name, int age, String gender,String breed) {this.name = name;this.age = age;this.gender = gender;this.breed=breed;}public void sleep(){System.out.println(breed+":"+name+"正在睡覺");}public void eat(String food){System.out.println(breed+":"+name+"正在吃"+food);}
}
cat是子類之一??
package com.wbc.opp.oop.day3.inherit;public class Cat extends Animal{public void catchMice(){System.out.println(getName()+":正在抓老鼠");}
}
package com.wbc.opp.oop.day3.inherit;public class TestCat {public static void main(String[] args) {Cat cat=new Cat();cat.setName("Tom");cat.setBreed("抓老鼠的貓");cat.setGender("公");cat.setAge(6);cat.catchMice();cat.printInfo();}
}
dog是另一子類?
package com.wbc.opp.oop.day3.inherit;public class Dog extends Animal {public void lookHouse(){System.out.println(getName()+":正在看家");}@Override//注解標簽。表示此方法是從父類重寫來的,檢查重寫是不是正確public void sleep(){System.out.println("狗在睡覺");}
}
package com.wbc.opp.oop.day3.inherit;public class TestDog {public static void main(String[] args) {Dog dog=new Dog();dog.setName("viky");dog.setBreed("泰迪");dog.setGender("母");dog.setAge(6);dog.lookHouse();dog.printInfo();dog.eat("狗糧");}
}
哮天犬又是dog的子類
package com.wbc.opp.oop.day3.inherit;public class Xtd extends Dog{public void fly(){System.out.println("哮天犬可以飛");}//可以左鍵快速重寫@Overridepublic void sleep() {super.sleep();//使用super調用父類方法System.out.println("哮天犬不需要睡覺");}}
package com.wbc.opp.oop.day3.inherit;public class TestXtq {public static void main(String[] args) {Xtd xtd=new Xtd();xtd.setName("哮天犬");}
}
三、抽象類
抽象方法:只有方法聲明,沒有具體實現的方法 why要有抽象方法:在一些體系結構的頂端類中,有些功能沒必要實現因為在不同子類中的實現都不相同,這時候就可以將方法聲明為抽線方法public abstract void eat(); //定義為抽象方法,沒有具體實現,不完整此時需要將類定義成抽象類public abstract class Animal {//將類使用abstract抽象化使用abstract關鍵字修飾的類就是抽象類。若一個類中有抽象方法,則其必須是抽象類。但抽象類中也可以有非抽象方法 抽象類:一個類中有不完整的方法,則這個類是抽象類特點:除了其不可構造對象外,其他功能與其他正常的類都相同,可以有變量,方法,構造器其子類必須重寫其所有抽象方法,或者也定義為抽象類作用:主要在上層定義功能,讓子類繼承實現語法:public abstract class Animal {//將類使用abstract抽象化
例:
父類Animal(抽象類)——》子類 cat(抽象類)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ——》另一子類 dog(非抽象)
package com.wbc.opp.oop.day3.Abstract;public abstract class Animal {//將類使用abstract抽象化private String name;private int age;public abstract void eat(String food); //定義為抽象方法public Animal(){}public Animal(String name, int age) {this.name = name;this.age = age;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public void printINfo(){System.out.println(name+"\t"+age);}
}
?
package com.wbc.opp.oop.day3.Abstract;public abstract class cat extends Animal {//定義為抽象類
}
package com.wbc.opp.oop.day3.Abstract;public class Dog extends Animal{public Dog() {}public Dog(String name, int age) {super(name, age);}@Overridepublic void eat(String food) {//重寫實現System.out.println(getName()+"吃"+food);}
}
package com.wbc.opp.oop.day3.Abstract;public class TestDog {public static void main(String[] args) {Dog dog=new Dog();dog.setName("旺財");dog.setAge(10);dog.printINfo();dog.eat("狗糧");}
}
四、練習
1.求學生平均成績 編寫出一個通用的人員類(Person),該類具有姓名(Name)、年齡(Age)、性別(gender)等。 然后對Person類的繼承得到一個學生類(Student), 該類能夠存放學生的5門課的成績(語文,數學,英語,物理,地理),并有一個方法能求出平均成績。 最后在main函數中對Student類的功能進行驗證。
//person類
package com.wbc.opp.oop.homework.day3.gradeAverage;public class Person {private String Name;private int Age;private String gender;public Person(String name, int age, String gender) {Name = name;Age = age;this.gender = gender;}public Person() {}public String getName() {return Name;}public void setName(String name) {Name = name;}public int getAge() {return Age;}public void setAge(int age) {Age = age;}public String getGender() {return gender;}public void setGender(String gender) {this.gender = gender;}}
//student類
package com.wbc.opp.oop.homework.day3.gradeAverage;public class Student extends Person {private double gradeAverage;private double chnAverage;private double mathAverage;private double engAverage;private double phyAverage;private double geoAverage;public Student() {super();}private Student(String name, int age, String gender) {super(name, age, gender);}private void setChnAverage(double chnAverage) {this.chnAverage = chnAverage;}private void setMathAverage(double mathAverage) {this.mathAverage = mathAverage;}private void setEngAverage(double engAverage) {this.engAverage = engAverage;}private void setPhyAverage(double phyAverage) {this.phyAverage = phyAverage;}private void setGeoAverage(double geoAverage) {this.geoAverage = geoAverage;}public void setAllAverage(double chnAverage,double mathAverage,double engAverage,double phyAverage,double geoAverage){setChnAverage(chnAverage);setMathAverage(mathAverage);setEngAverage(engAverage);setPhyAverage(phyAverage);setGeoAverage(geoAverage);}public void printInfo(){System.out.println("語文:"+chnAverage);System.out.println("數學:"+mathAverage);System.out.println("英語:"+engAverage);System.out.println("物理:"+phyAverage);System.out.println("地理:"+geoAverage);}public double grade(){return (chnAverage+mathAverage+engAverage+phyAverage+geoAverage)/5;}
}
//測試類
package com.wbc.opp.oop.homework.day3.gradeAverage;public class TestStudent {public static void main(String[] args) {Student stu=new Student();stu.setAllAverage(1,2,3,4,5);stu.printInfo();System.out.println("平均成績為:"+stu.grade());}
}
?
2.人類設計 定義一個人類,包括屬性:姓名、性別、年齡、國籍;包括方法:吃飯、睡覺,工作。 (1)根據人類,派生一個學生類,增加屬性:學校、學號;重寫工作方法(學生的工作是學習)。 (2)根據人類,派生一個工人類,增加屬性:單位、工齡;重寫工作方法(工人的工作是……自己想吧)。 (3)根據學生類,派生一個學生干部類,增加屬性:職務;增加方法:開會。
//Person類
package com.wbc.opp.oop.homework.day3.human;public abstract class Person {private String name;private String gender;private int age;private String country;public Person() {}public Person(String name, String gender, int age, String country) {this.name = name;this.gender = gender;this.age = age;this.country = country;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getGender() {return gender;}public void setGender(String gender) {this.gender = gender;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public String getCountry() {return country;}public void setCountry(String country) {this.country = country;}public void setAllInfo(){System.out.println(name);System.out.println(gender);System.out.println(age);System.out.println(country);;}public void eat(){System.out.println(getName()+"吃飯");}public void sleep(){System.out.println(getName()+"睡覺");}public abstract void work();
}
//Student類
package com.wbc.opp.oop.homework.day3.human;public class Student extends Person{private String school;private String stuNums;public Student() {}public Student(String name,String gender,int age,String country,String school,String stuNums) {super(name, gender, age, country);this.school = school;this.stuNums = stuNums;}public String getSchool() {return school;}public void setSchool(String school) {this.school = school;}public String getStuNums() {return stuNums;}public void setStuNums(String stuNums) {this.stuNums = stuNums;}public void getAllInfo(){System.out.println(getName());System.out.println(getGender());System.out.println(getAge());System.out.println(getCountry());System.out.println(school);System.out.println(stuNums);}@Overridepublic void work() {System.out.println(getName()+"學習");}
}
//ITworker類
package com.wbc.opp.oop.homework.day3.human;public class ITworker extends Person{private String unit;private int workAge;public ITworker() {}public ITworker(String name, String gender, int age, String country, String unit, int workAge) {super(name, gender, age, country);this.unit = unit;this.workAge = workAge;}public String getUnit() {return unit;}public void setUnit(String unit) {this.unit = unit;}public int getWorkAge() {return workAge;}public void setWorkAge(int workAge) {this.workAge = workAge;}public void setAllInfo(String unit,int workAge){setUnit(unit);setWorkAge(workAge);}public void getAllInfo(){System.out.println(getName());System.out.println(getGender());System.out.println(getAge());System.out.println(getCountry());System.out.println(unit);System.out.println(workAge);}@Overridepublic void work() {System.out.println(getName()+"正在賽博板磚");}
}
//Stucadre學生干部類
package com.wbc.opp.oop.homework.day3.human;public class StuCadre extends Student{private String post;public StuCadre() {}public StuCadre(String name, String gender, int age, String country, String school, String stuNums, String post) {super(name, gender, age, country, school, stuNums);this.post = post;}public void getAllInfo(){System.out.println(getName());System.out.println(getGender());System.out.println(getAge());System.out.println(getCountry());System.out.println(getSchool());System.out.println(getStuNums());System.out.println(post);}public void meeting(){System.out.println(post+getName()+"正在開會");}
}
//test類
package com.wbc.opp.oop.homework.day3.human;public class Test {public static void main(String[] args) {Student stu =new Student("wang","男",20,"中國","snut","0001");stu.work();stu.eat();stu.sleep();stu.getAllInfo();ITworker worker =new ITworker("wang","男",20,"中國","非凡",1);worker.work();worker.getAllInfo();StuCadre cadre=new StuCadre("wang","男",20,"中國","snut","0001","學生會主席");cadre.work();cadre.getAllInfo();cadre.meeting();}
}
3、定義一個人類 , 包含一個country屬性,speak(),eat()方法 派生一個中國人和美國人類, 兩種人說話方式語言不同, 兩種人吃飯方式不同
//person類
package com.wbc.opp.oop.homework.day3.personCountry;public abstract class Person {private String country;public Person(String country) {this.country = country;}public Person() {}public String getCountry() {return country;}public void setCountry(String country) {this.country = country;}public abstract void speak();public abstract void eat();
}
//Chinese類
package com.wbc.opp.oop.homework.day3.personCountry;public class Chinese extends Person{public Chinese(String country) {super(country);}@Overridepublic void speak() {System.out.println(getCountry()+"人說中文");}@Overridepublic void eat() {System.out.println(getCountry()+"人吃中餐");}
}
//American類
package com.wbc.opp.oop.homework.day3.personCountry;public class American extends Person{public American(String country) {super(country);}@Overridepublic void speak() {System.out.println(getCountry()+"人說鳥語");}@Overridepublic void eat() {System.out.println(getCountry()+"人吃西餐");}
}
//test類
package com.wbc.opp.oop.homework.day3.personCountry;public class Test {public static void main(String[] args) {Chinese chinese=new Chinese("中國");chinese.eat();chinese.speak();American american=new American("美國");american.eat();american.speak();}
}
4.學生信息管理系統 設計一個學生信息管理系統,有添加學生,查詢學生,刪除學生等功能. 要求:1.設計一個類學生類,學生屬性有學號,姓名,性別(屬性私有權限) 用來存儲學生的信息 要求2:實現對學生信息的增刪查操作 要求3:使用一個數組存儲學生信息,數組上限定為30即可.
啟動程序后輸出一個菜單讓用戶選擇操作:1.添加學生,2-刪除學生,3.查詢學生,4-退出
選擇添加學生在控制臺依次輸入學號,姓名,性別等信息,將學生信息存儲到一個學生對象中,
并將學生對象存儲到數組中,數組容量定為30.
刪除時,必須輸入學號,如果學號對應的學生存在,從數組中刪除該學生信息
點擊查詢時,必須輸入學號,如果學號對應的學生存在,輸出學生信息即可,不存在,輸出學號有誤
//Student類
package com.wbc.opp.oop.homework.day3.studentInformationManagementSystem;/*學生類,主要作用,存儲學生信息*/
public class Student {private int num;private String name;private String gender;public int getNum() {return num;}public void setNum(int num) {this.num = num;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getGender() {return gender;}public void setGender(String gender) {this.gender = gender;}@Overridepublic String toString() {return "Student{" +"num=" + num +", name='" + name + '\'' +", gender='" + gender + '\'' +'}';}
}
//StudentManage類
package com.wbc.opp.oop.homework.day3.studentInformationManagementSystem;import java.util.Scanner;/*學生信息管理類在此類中,可以有添加,刪除,查詢學生信息等功能*/
public class StudentManage {Student[] students = new Student[30];//[null,null,null]static int count=0;/*管理系統主方法*/public void menu(){loop:while(true){System.out.println("用戶選擇操作: 1.添加學生,2-刪除學生,3.查詢學生,4-退出");Scanner s = new Scanner(System.in);int item = s.nextInt();switch (item){case 1: this.add(); break;case 2: this.delete(); break;case 3: this.search(); break;case 4: break loop;}}}/*添加
*/public void add() {Student stu = new Student();Scanner name = new Scanner(System.in);System.out.println("請輸入姓名");String names = name.next();stu.setName(names);Scanner num = new Scanner(System.in);System.out.println("請輸入學號");int nums = num.nextInt();stu.setNum(nums);Scanner gender = new Scanner(System.in);System.out.println("請輸入性別");String genders = gender.next();stu.setGender(genders);for (int i = 0; i < students.length; i++) {if (students[i] == null) {students[i] = stu;count++;break;}}System.out.println("添加成功");}/*刪除*/public void delete() {System.out.println("請輸入學生學號");Scanner num = new Scanner(System.in);int nums = num.nextInt();for (int i = 0; i < students.length; i++) {if (students[i] != null && students[i].getNum() == nums) {for (int j = i; j < students.length - 1; j++) {students[j] = students[j + 1];}count--;System.out.println("刪除成功");return;}}System.out.println("查無此人");}/*查詢*/public void search() {System.out.println("請輸入學生學號");Scanner num = new Scanner(System.in);int nums = num.nextInt();for (int i = 0; i < students.length; i++) {if (students[i] != null && students[i].getNum() == nums) {System.out.println(students[i].toString());return;}}System.out.println("查無此人");}}
//RunStudentManage類
package com.wbc.opp.oop.homework.day3.studentInformationManagementSystem;/*啟動類*/
public class RunStudentManage {public static void main(String[] args) {/*創建一個學生信息管理系統對象*/StudentManage studentManager = new StudentManage();studentManager.menu();}
}