需求三:
已知猫类和狗类:
属性:毛的颜色,腿的个数
行为:吃饭
猫特有行为:抓老鼠catchMouse
狗特有行为:看家lookHome
public class Demo1 {
public static void main(String[] args) {
Cat cat=new Cat("红", 4);//创建猫对象
cat.eat();//调用吃方法
cat.catchMouse();//调用捉老鼠
Dog dog=new Dog("蓝", 4);//创建狗对象
dog.eat();//调用吃方法
dog.lookHome();//调用看家方法
}
}
class Animal{
private String color;
private int leg;
public Animal() {
super();
}
public Animal(String color, int leg) {
super();
this.color = color;//毛的颜色
this.leg = leg;//腿的个数
}
//吃饭
public void eat() {
// TODO Auto-generated method stub
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public int getLeg() {
return leg;
}
public void setLeg(int leg) {
this.leg = leg;
}
}
class Cat extends Animal{
public Cat(String color, int leg) {
super(color,leg);
}
public Cat() {
super();
}
//捉老鼠
public void catchMouse() {
System.out.println("捉老鼠");
}
}
class Dog extends Animal{
public Dog(String color, int leg) {
super(color,leg);
}
public Dog() {
super();
}
//看家
public void lookHome() {
System.out.println("看家");
}
} |
|