/*
多态
要有继承,方法重写,父类引用指向子类对象.
*/
class Demo_Animal {
public static void main(String[] args)
{
Animal a = new Cat(); //父类引用指向子类对象.
a.eat();
}
}
class Animal {
private String name;
private int age;
public Animal(){}
public Animal(String name,int age){
this.name = name;
this.age = age;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setAge(int age) {
this.age = age;
}
public int getAge() {
return age;
}
public void eat(){
System.out.println("吃饭");
}
}
class Cat extends Animal { //继承(成员方法)
public void eat(){ //方法重写
System.out.println("猫吃鱼");
}
}
|
|