/*
具体事物:猫,狗
* 共性:姓名,年龄,吃饭
* 猫的特性:抓老鼠
* 狗的特性:看家
*/
class Demo02_Polymorphic
{
public static void main(String[] args)
{
Cat c=new Cat("加菲猫",6);
System.out.println(c.getName()+c.getAge());
c.eat();
c.catchMouse();
}
}
abstract 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 abstract void eat();
}
class Cat extends Animal{
public Cat(){}
public Cat(String name,int age){
super(name,age);
}
public void eat(){
System.out.println("猫吃鱼");
}
public void catchMouse(){
System.out.println("猫抓老鼠");
}
}
class Dog extends Animal{
public Dog(){}
public Dog(String name,int age){
super(name,age);
}
public void eat(){
System.out.println("狗吃肉");
}
public void watchHome(){
System.out.println("看家");
}
}
|
|