一、题目:
二、代码实现
Animals类
Dog类
Cat类
Person类
test类
三、具体思路
1.画出继承结构,写出父类Animals,Person类不在继承结构中。父类包括:共有的私有化成员变量、空参构造、全参构造、set,get方法、行为(吃东西)(此行为在Dog和Cat类中进行重写)
2.写Dog类,包括:空参、全参、eat重写方法、lookHome行为
public Dog(int age ,String color) {
super(age,color);
}
3.Cat类:与Dog类类似
4.Person类:私有化成员变量、空参构造、全参构造、set,get方法、行为keepPet
5.test类:创建Person对象,将参数传递,因此方法参数包含Dog类型和Cat类型,所以需创建Dog对象和Cat对象,并将对象d和c作为参数传给各自的keepPet方法
四、注意事项
1.Dog类代码第十行:getAge,getColor
2.dog.getColor():dog为一个对象