/*
动物园里有很多种动物:
比如说,狗,猫等。
狗有姓名和年龄,猫也有姓名和年龄。
狗有跑步的方法,猫也有跑步的方法。而且都仅仅是跑步。
狗有吃饭的方法,猫也有吃饭的方法。只不过,狗吃骨头,猫吃鱼
*/
class Animal
{
private String name;
private int age;
public Animal(){}
public Animal(String name,int age)
{
this.name = name;
this.age = age;
}
public void print()
{
System.out.println(name+"---------"+age+"岁");
}
public void eat()
{
if (name.equals("mao"))
{
System.out.println("吃鱼");
}
else if (name.equals("dog"))
{
System.out.println("啃骨头");
}
else
System.out.println("这个小动物去火星旅游去了");
}
}
class Cat extends Animal
{
public Cat(){}
public Cat(String name,int age)
{
super(name,age);
}
public void run()
{
System.out.println("小猫跑步");
}
}
class Dog extends Animal
{
public Dog(){}
public Dog(String name,int age)
{
super(name,age);
}
public void run()
{
System.out.println("小狗跑步");
}
}
class Mouse extends Animal
{
public Mouse(String name,int age)
{
super(name,age);
}
}
class AnimalTest
{
public static void main(String[] args)
{
Cat c = new Cat("mao",2);
c.print();
c.run();
c.eat();
System.out.println("--------------------");
Dog d = new Dog("dog",2);
d.print();
d.run();
d.eat();
System.out.println("--------------------");
Mouse m = new Mouse("mouse",2);
m.eat();
System.out.println("--------------------");
}
}
|
|