已知猫类和狗类:
属性:毛的颜色,腿的个数
行为:吃饭
猫特有行为:抓老鼠catchMouse
狗特有行为:看家lookHome
class Text1 {
public static void main(String[] args) {
Cat c = new Cat("花",8);
System.out.println(c.getColor()+" "+c.getLeg());
c.eat();
c.catchMouse();
Dog d = new Dog("黑",12);
System.out.println(d.getColor()+" "+d.getLeg());
d.eat();
d.lookHome();
}
}
abstract class Animal{
private String color;
private int leg;
public Animal(){}
public Animal(String color,int leg){
this.color= color;
this.leg = leg;
}
public void setColor(String color){
this.color = color;
}
public String getColor(){
return color;
}
public void setLeg(int leg){
this.leg = leg;
}
public int getLeg(){
return leg;
}
public abstract void eat();
}
class Cat extends Animal{
public Cat(){}
public Cat(String color,int leg){
super(color,leg);
}
public void eat(){
System.out.println("猫吃鱼");
}
public void catchMouse(){
System.out.println("抓老鼠");
}
}
class Dog extends Animal{
public Dog(){}
public Dog(String color,int leg){
super(color,leg);
}
public void eat(){
System.out.println("狗吃肉");
}
public void lookHome(){
System.out.println("看家");
}
} |
|