super
一、理解
super.屬性:在子類中,調用父類非私有化的成員屬性
super.方法:在子類中,調用父類非私有化的成員方法
super():在子類構造方法中調用父類非私有的構造方法
二、案例
需求:編寫中國人和日本人的類
分析:
? 人類:
? 屬性:姓名、性別、年齡
? 方法:吃飯飯、睡覺覺
? 中國人類 繼承 人類:
? 屬性:身份證
? 方法:打太極
? 日本人類 繼承 人類:
? 屬性:年號
? 方法:拍電影
1、主方法入口
package com.xx.test03;public class Test01 {public static void main(String[] args) {Chinese c = new Chinese("侯小康", '男', 23, "12345678901");c.eat();c.sleep();c.playTaiJi();System.out.println("------------------------------");Japanese j = new Japanese("波多野結衣", '女', 18, "令和");j.eat();j.sleep();j.playVedio();}
}
2、Person-父類
package com.qf.test03;public class Person {//私有化屬性
private String name;
private char sex;
private int age;
//無參數構造方法
public Person() {
}
//有參數構造方法
public Person(String name, char sex, int age) {this.name = name;this.sex = sex;this.age = age;
}
//set/get方法
public String getName() {return name;
}public void setName(String name) {this.name = name;
}public char getSex() {return sex;
}public void setSex(char sex) {this.sex = sex;
}public int getAge() {return age;
}public void setAge(int age) {this.age = age;
}public void eat(){System.out.println(this.name + "吃飯飯");
}public void sleep(){System.out.println(this.name + "睡覺覺");}
}
3、Chinese-子類
package com.xx.test03;public class Chinese extends Person{private String id;public Chinese() {}public Chinese(String name, char sex, int age, String id) {super(name, sex, age);this.id = id;}public String getId() {return id;}public void setId(String id) {this.id = id;}public void playTaiJi(){System.out.println(super.getName() + "打太極");}}
4、 Indians -子類
package com.xx.test03;public class Indians extends Person{
private String yearNum;public Indians() {
}public Indians(String name, char sex, int age, String yearNum) {super(name, sex, age);this.yearNum = yearNum;
}public String getYearNum() {return yearNum;
}public void setYearNum(String yearNum) {this.yearNum = yearNum;
}public void playVedio(){System.out.println(super.getName() + "拍電影");}
}