黑马程序员技术交流社区
标题:
继承-案例共享
[打印本页]
作者:
飞翔锐翼
时间:
2015-8-30 23:23
标题:
继承-案例共享
class Mao {
public static void main(String[] args) {
Cat c1 = new Cat("白",4);
c1.eat();
c1.catchMouse();
System.out.println(c1.getColor()+" "+c1.getLeg());
Dog c2 = new Dog("黑",2);
c2.eat();
c2.lookHome();
System.out.println(c2.getColor()+" "+c2.getLeg());
}
}
class Animal {
String color;
int leg;
public Animal (){}
public Animal(String color ,int leg){
this.color = color;
this.leg = leg;
}
public void setColor(){
this.color = color;
}
public String getColor(){
return color;
}
public void setLeg(){
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 catchMouse(){
System.out.println("抓老鼠");
}
}
class Dog extends Animal {
public Dog(){}
public Dog(String color,int leg){
super(color,leg);
}
public void lookHome(){
System.out.println("看家");
}
}
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2