标题: 学不会了 [打印本页] 作者: 王海平 时间: 2016-1-22 23:53 标题: 学不会了 class Demo {
public static void main(String[] args){
Cat c=new Cat("黄",5);
System.out.println(c.getColor()+"\n"+c.getLeg());
c.catchMouse();
Dog d=new Dog("绿",3);
System.out.println(d.getColor()+"\n"+d.getLeg());
d.lookHome();
}
}
class Animal {
String color;
int leg;
public Animal(){}
public Animal(String color,int age){
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;
}
}
class Cat extends Animal {
public Cat(){}
public Cat(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 catchMouse(){
System.out.println("抓老鼠");
}
}
class Dog extends Animal {
public Dog(){}
public Dog(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 lookHome(){
System.out.println("看门");
}
}