/*
思路:
分析类:狗,猫,动物,测试类
狗:姓名,年龄,跑步,吃饭(吃骨头)
猫:姓名,年龄,跑步,吃饭(吃鱼)
动物:姓名,年龄,跑步,吃饭
测试类:main方法
分析:狗和猫都有共同的属性和方法,他们都具备动物所以得特性和行为
*/
class Animal
{
private String name;
private int age;
private String eat;
public Animal(){}
public Animal(String name,int age,String eat)
{
this.name=name;
this.age=age;
this.eat=eat;
System.out.println(name+".."+age+"岁了,喜欢吃"+eat);
}
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 run()
{
System.out.println("run");
}
public void eat()
{
System.out.println("eat");
}
public void show()
{
System.out.println("name:"+name+"age:"+age);
}
}
class Dog extends Animal
{
public Dog(String name,int age,String eat)
{
super(name,age,eat);
}
public void eat()
{
System.out.println("喜欢吃骨头");
}
}
class Cat extends Animal
{
public Cat(String name,int age,String eat)
{
super(name,age,eat);
}
public void eat()
{
System.out.println("喜欢吃鱼");
}
}
class AnimalTest
{
public static void main(String[] args)
{
Dog d=new Dog("豆豆",1,"骨头");
//d.run();
//d.eat();
//d.show();
Cat c=new Cat("苗苗",2,"鱼");
//c.run();
//c.eat();
//c.show();
}
}
|
|