在父类中创建的方法并不满足当前需求,所以需要在子类中实现方法重写,重新对当前方法进行定义。
注意的是:
子类根据需求对从父类继承的方法进行重新编写重写时,
可以用super.方法的方式来保留父类的方法
构造方法不能被重写
方法重写规则1、方法名相同
2、参数名相同
3、返回值类型相同或者是其子类
4、访问权限不能严于父类
5、父类的静态方法不能被子类覆盖为非静态方法,父类的非静态方法不能被子类覆盖为静态方法
6、子类可以定义与父类同名的静态方法,以便在子类中隐藏父类的静态方法(注:静态方法中无法使用super)
7、父类的私有方法不能被子类覆盖
8、不能抛出比父类方法更多的异常
多态方法重写是实现多态的基础
toHospital()不需要有具体的实现 抽象方法 Animal类声明为抽象类 实例化Animal毫无意义public abstract class Animal {
}
下面用一个例子演示
1、定义父类animal
//定义父类animal
public abstract class Animal {
// 定义属性姓名name 健康值health 亲密度love
private String name;
private int health;
private int love;
// 无参构造方法
public Animal() {
super();// 继承父类Object的无参构造方法
}
// 有参构造方法
public Animal(String name, int health, int love) {
super();// 继承父类Object的有参构造方法
this.name = name;
this.health = health;
this.love = love;
}
// setXxx/getXxx 构造方法
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;
}
// 方法show
public void show() {
System.out.println("您的宠物:" + this.name);
System.out.println("健康值:" + this.health);
System.out.println("亲密度:" + this.love);
}
// 抽象方法 toHospital
public abstract void toHospital();
}
2、定义一个Animal的子类Dog类
//定义一个Animal的子类Dog类
public class Dog extends Animal {
// 定义属性种类breed
private String breed;
// 无参构造方法
public Dog() {
super();// 调用父类Animal的无参构造方法
}
// 有参构造方法
public Dog(String name, int health, int love, String breed) {
super(name, health, love);// 调用父类Animal的有参构造方法
this.breed = breed;
}
// setXxx/getXxx 构造方法
public String getBreed() {
return breed;
}
public void setBreed(String breed) {
this.breed = breed;
}
// 重写父类show方法
public void show() {
super.show();// 继承父类Animal的show方法
System.out.println("您的宠物种类:" + this.breed);
}
// 重写父类toHospital方法(多态)
public void toHospital() {
System.out.println("打针吃药");
this.setHealth(90);
}
}
3、定义一个Animal的子类Car类
//定义一个Animal的子类Car类
public class Car extends Animal {
// 定义属性颜色color
private String color;
// 无参构造方法
public Car() {
super();// 调用父类Animal的无参构造方法
}
// 有参构造方法
public Car(String name, int health, int love, String color) {
super(name, health, love);// 调用父类Animal的有参构造方法
this.color = color;
}
// setXxx/getXxx 构造方法
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
// 重写父类show方法
public void show() {
super.show();// 继承父类Animal的show方法
System.out.println("您的宠物种类:" + this.color);
}
// 重写父类toHospital方法(多态)
public void toHospital() {
System.out.println("吃药疗养");
this.setHealth(90);
}
}
4、定义一个Master类
//定义一个Master类
public class Master {
public void cure(Animal animal) {
if (animal.getHealth() < 60) {
animal.toHospital();
}
}
}
5、测试
//创建测试类
public class Text {
public static void main(String[] args) {
// 创建master对象
Master master = new Master();
// 创建dog对象
Dog dog = new Dog("修狗", 50, 90, "哈士奇");
// 宠物现在的状态
dog.show();
System.out.println("----------------");
master.cure(dog);
System.out.println("----------------");
// 宠物看过病的状态
dog.show();
System.out.println("----------------");
// 创建Car对象
Car car = new Car("毛毛", 40, 88, "白色");
// 宠物现在的状态
car.show();
System.out.println("----------------");
master.cure(car);
System.out.println("----------------");
// 宠物看过病的状态
car.show();
}
}
运行结果



