需求:
已知猫类和狗类:
属性:毛的颜色,腿的个数
行为:吃饭
猫特有行为:抓老鼠catchMouse
狗特有行为:看家lookHome
利用面向对象继承法来做,代码如下:
公共父类
这一类是,他们都有颜色有腿,而且在吃饭
public class Pet {
private String name;
private String color;
private int leggs;
public Pet(String name,String color,int leggs) {
this.color=color;
this.leggs=leggs;
this.name=name;
}
public String getColor() {
return color;
}
public