已知猫类和狗类:
属性:毛的颜色,腿的个数
行为:吃饭
猫特有行为:抓老鼠catchMouse
狗特有行为:看家lookHome
class Text3 {
public static void main(String[] args) {
Cat c = new Cat("花",8);
c.show();
c.eat();
c.catchMouse();
Dog g = new Dog("啸天",16);
g.print();
g.eat();
g.lookHome();
}
}
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 void eat(){
System.out.println("吃饭");
}
}
class Cat extends Animal{
public Cat(){}
public Cat(String color,int leg){
super(color,leg);
}
public void show(){
System.out.println(getColor()+" "+getLeg());
}
public void catchMouse(){
System.out.println("抓老鼠");
}
}
class Dog extends Animal{
public Dog(){}
public Dog(String color,int leg){
super(color,leg);
}
public void print(){
System.out.println(getColor()+" "+getLeg());
}
public void lookHome(){
System.out.println("看家");
}
}
|
|