封裝:
屬性私有:將屬性使用private修飾,表示此屬性只能在本類中存取,不讓外部直接存取
方法公開:針對每一個屬性編寫一對公開的方法 get set 分別用於屬性的取值/賦值
package com.atguigu.demo.demo1;
public class Test1 {
public static void main(String[] args) {
Rabbbit rabbbit = new Rabbbit();
rabbbit.setName("小白兔");
rabbbit.setHealth(-19);
rabbbit.setAppetite(1111);
rabbbit.setColor("白色");
}
}
class Rabbbit{
private String name;//名字
public void setName(String name){
this.name = name;
}
public String getName(){
return name;
}
private String color;//顏色
public void setColor(String color) {
if((!color.equals("黑"))||(!color.equals("白"))||(!color.equals("灰"))){
System.out.println("您輸入的顏色太花哨,"+color+"我們將預設設定為:白色");
this.color="白";
}else{
this.color = color;
}
}
public String getColor() {
return color;
}
private int health;//健康值
public void setHealth(int health) {
if(((health<0)||(health>100))){
System.out.println("您輸入的健康值不合法,"+health+"我們將預設設定為:60");
this.health=60;
}else{
this.health = health;
}
}
public int getHealth() {
return health;
}
private double appetite;//食量
public void setAppetite(double appetite) {
if(((appetite<0)||(appetite>100))){
System.out.println("您輸入的健康值不合法,"+appetite+"我們將預設設定為:60");
this.appetite=10;
}else{
this.appetite = appetite;
}
}
public double getAppetite() {
return appetite;
}
}
便於使用者正確使用系統,防止錯誤修改屬性
降低了構建大型系統的風險
提高程式的可重用性
降低程式之間的耦合度
類的存取許可權修飾符有兩個:
public :表示在本專案中任何位置都可以存取
預設不寫:表示在同包中才能存取
- 類成員存取修飾符:類的成員包括屬性、方法、構造方法、程式碼塊
- private:本類
- 預設不寫:本包/同包
- protected:本類、本包、子類
- public:任何位置
自定義型別的屬性,參照資料型別。
自定義型別的陣列 ,依然屬於參照資料型別,使用方式與之前一致
package com.atguigu.demo.demo3;
public class Person {
private String name;
private String sex;
private int age;
private Address address;
private Hobby[] habby;
public Person() {
}
public Person(String name, String sex, int age, Address address, Hobby[] habby) {
this.name = name;
this.sex = sex;
this.age = age;
this.address = address;
this.habby = habby;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public Hobby[] getHabby() {
return habby;
}
public void setHabby(Hobby[] habby) {
this.habby = habby;
}
}
package com.atguigu.demo.demo3;
public class Address {
private String province;
private String city;
private String street;
private String buildNo;
public Address() {
}
public Address(String province, String city, String street, String buildNo) {
this.province = province;
this.city = city;
this.street = street;
this.buildNo = buildNo;
}
public String getProvince() {
return province;
}
public void setProvince(String province) {
this.province = province;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public String getBuildNo() {
return buildNo;
}
public void setBuildNo(String buildNo) {
this.buildNo = buildNo;
}
}
package com.atguigu.demo.demo3;
public class Hobby {
private String type;
private String name;
public Hobby() {
}
public Hobby(String type, String name) {
this.type = type;
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package com.atguigu.demo.demo3;
public class TestPerson {
public static void main(String[] args) {
Person p1 = new Person();
p1.setName("張麻子");
p1.setSex("男");
p1.setAge(32);
Address a1 = new Address("重慶市","江津區","龍華鎮","鵝城");
p1.setAddress(a1);
Hobby[] hobbis=new Hobby[3];
hobbis[0] = new Hobby("體育","跑步");
hobbis[1] = new Hobby("電競","天涯明月刀");
hobbis[2] = new Hobby("音樂" ,"周杰倫");
p1.setHabby(hobbis);
System.out.println("姓名:"+p1.getName());
System.out.println("性別:"+p1.getSex());
System.out.println("年齡:"+p1.getAge());
System.out.println("家庭住址:"+p1.getAddress().getProvince()+
p1.getAddress().getCity()+p1.getAddress().getStreet()+p1.getAddress().getBuildNo());
int way = 1;
for (int i = 0; i < hobbis.length; i++) {
System.out.println("第"+(way++)+"個愛好是:"+hobbis[i].getType()+"類:"+hobbis[i].getName());
}
}
}
子類繼承父類別,子類與父類別是is-a的關係
Java中只支援單根繼承,即一個類只能有一個直接父類別,間接父類別不設限
繼承public和protected修飾的屬性和方法,不管子類和父類別是否在同一個包裡
繼承預設許可權修飾符修飾的屬性和方法,但子類和父類別必須在同一個包裡
private修飾的屬性和方法不能被繼承
private成員
子類與父類別不在同包,使用預設存取許可權的成員
構造方法(
構造方法用於給子類存取,不屬於繼承
)
super關鍵字:表示父類別的物件
可以存取父類別的屬性、方法、構造方法
回顧之前我們呼叫父類別print方法,存在的兩個問題:
1.因為父類別中只有共有的屬性,所以列印寵物資訊不完善
2.父類別中對寵物身份描述不具體
我們通過super關鍵字存取父類別的方法,可以解決第一個小問題,相當於功能的組合
無參構造方法預設存在,JVM提供的,如果書寫了有參構造,無參構造將被覆蓋,如需使用,必須顯式書寫。
1.子類構造方法預設存取父類別的無參構造方法,除非子類顯式的存取父類別的有參構造方法
2.子類構造方法必須存取父類別無參或者有參構造其中一個
3.存取父類別的構造方法,並不會建立父類別物件,存取父類別構造方法智慧初始化父類別中的屬
package com.atguigu.demo.demo1;
public class Animal {
protected String name;
protected int age;
protected String sex;
public Animal() {
}
public Animal(String name, int age, String sex) {
this.name = name;
this.age = age;
this.sex = sex;
}
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 getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public void printAnimal(){
System.out.println("This animal name's :"+ name );
System.out.println("This animal age is :"+ age );
System.out.println("This animal sex is :"+ sex );
}
}
package com.atguigu.demo.demo1;
/**
* super關鍵字
*/
public class Dog extends Animal{
private String dogType;
public Dog() {
}
public Dog(String name,int age,String sex ,String dogType) {
super(name,age,sex); //呼叫父類別構造
this.dogType = dogType;
}
public String getDogType() {
return dogType;
}
public void setDogType(String dogType) {
this.dogType = dogType;
}
public void printDog() {
super.printAnimal(); //呼叫父類別方法
// System.out.println(super.name);//呼叫父類別屬性
System.out.println("This dog's TYPE is"+dogType);
}
}