1、继承的概念
-
多个类中相同的属性和方法抽取出来放入到一个新的类中(父类),
-
多个类中就不需要再重复定义这些属性和方法,只需要写自己类中独有的属性和方法,使用extends关键字继承父类,就可以使用父类中的属性和方法
示例:
1、构造父类Pet
package cn.bdqn.demo04;
public class Pet {
private String name;
private int health;
private int love;
// 添加构造方法
public Pet() {
}
public Pet(String name, int health, int love) {
this.name = name;
this.health = health;
this.love = love;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getHealth() {
return health;
}
public void setHealth(int health) {
this.health = health;
}
public int getLove() {
return love;
}
public void setLove(int love) {
this.love = love;
}
public void print() {
System.out.println("宠物信息:昵称:" + this.getName() + ",健康值:"
+ this.getHealth() + ",亲密度:" + this.getLove());
}
}
2、构造子类Cat并用Cat extends Pet继承父类:
package cn.bdqn.demo04;
public class Cat extends Pet {
// 定义Cat类独有的属性
private String color;
public Cat() {
super();// 调用父类Pet类里的无参构造方法
}
public Cat(String name, int health, int love, String color) {
super(name, health, love);
this.color = color;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
}
3、构造子类Dog并用Dog extends Pet继承父类:
package cn.bdqn.demo04;
public class Dog extends Pet {
//strain属性是DOg类里独有的属性,其它类没有,所以要在Dog类里定义
private String strain;
public Dog() {
}
public Dog(String name, int health, int love, String strain) {
super(name, health, love);
this.strain = strain;
}
public String getStrain() {
return strain;
}
public void setStrain(String strain) {
this.strain = strain;
}
}
4、创建测试类Test
package cn.bdqn.demo04;
public class Test {
public static void main(String[] args) {
// 创建Dog类对象
Dog dog1 = new Dog("旺财", 99, 99, "金毛");
//在Dog类中没有setName()方法,但是DOg类对象dog1是可以调用,因为Dog类继承了Pet类,所以dog1可以调用父类Pet类里的setName()方法
dog1.setName("来福");
dog1.print();
//创建Cat类对象
Cat cat1 = new Cat("Tom", 100, 80, "蓝色");
cat1.print();
//hashCode()在Cat里没有、在Cat的父类Pet类里也没有,hashCode()方法存在于Object类中,Object类是Pet类的默认父类,所以Cat类对象可以使用Pet类里的方法,也可以使用Pet类的父类里的方法
//声明一个类,如果没有指定其父类是谁,系统会让此类默认去继承Object类
int num =cat1.hashCode();
System.out.println(num);//1694203642
}
}
2、super关键字
-
super关键字来访问父类的成员(super表示父类的对象)
-
super只能出现在子类的方法和构造方法中
-
super调用构造方法时,只能是第一句(super在构造方法中要处于第一行)
-
super不能访问父类的private成员
-
super只能存在于子类的方法和构造方法中,可以调用父类的属性、方法、构造方法
示例:
1、父类:
package cn.bdqn.demo05;
public class Father {
private String name;
private int age;
public double height;
public Father() {
super();//调用Father类的父类Object类的无参构造方法
System.out.println("我是Father类里的无参构造方法");
}
public Father(String name, int age,double height) {
super();//调用Father类的父类Object类的无参构造方法
this.name = name;
this.age = age;
this.height = height;
System.out.println("我是Father类里的有参构造方法");
}
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;
}
private void test(){
System.out.println("我是Father类里的私有test()方法");
}
public void print(){
System.out.println("我是Father类里的公共print()方法");
}
}
2、子类
package cn.bdqn.demo05;
public class Son extends Father {
private double money;
public Son() {
super();//调用的Son类的父类Father类里的无参构造方法
System.out.println("我是Son类里的无参构造方法");
}
public Son(String name, int age, double height, double money) {
super(name, age, height);//调用Son类的父类Father类里的有参构造方法
this.money = money;
System.out.println("我是Son类里的有参构造方法");
}
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public void printInfo(){
//输出父类里的属性
//super不能直接调用父类里被private修饰的属性
// System.out.println(super.name);
// System.out.println(super.age);
System.out.println(super.height);
//super不能直接调用父类里被private修饰的方法
// super.test();
super.print();
}
}
3、测试类
package cn.bdqn.demo05;
public class Test {
public static void main(String[] args) {
// 使用Son类的无参构造方法创建对象
Son son1 = new Son();
//创建Son类对象一共调用了几次构造方法??? 3次
System.out.println("----------------");
Son son2 = new Son("张三", 22, 173, 999);
son2.printInfo();
}
}
3、继承条件下构造方法的调用规则
-
子类构造方法没有通过super显式调用父类的有参构造方法,也没通过this显式调用自身其他构造方法
------系统默认调用父类的无参构造方法
-
子类构造方法通过super显式调用父类的有参构造方法
-------执行父类相应构造方法,而不执行父类无参构造方法
-
子类构造方法通过this显式调用自身的其他构造方法,在相应构造方法中应用以上两条规则