黑马程序员技术交流社区
标题:
刚刚写的
[打印本页]
作者:
wx_pmVIrag6
时间:
2016-7-31 21:38
标题:
刚刚写的
class Demo {
public static void main(String[] args) {
Cat sc = new Cat();
sc.setColor("白色");
sc.setLeg(4);
sc.show();
sc.eat();
sc.run();
System.out.println();
Dog st = new Dog("黑色", 4);
st.show();
st.eat();
sc.run();
}
}
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 this.color;
}
public void setLeg(int leg){
this.leg = leg;
}
public int getLeg(){
return this.leg;
}
public void eat(){
System.out.println("动物都是要吃东西的");
}
public void run(){
System.out.println("动物都是用腿跑的");
}
}
class Cat extends Animal{
public Cat(){}
public Cat(String color, int leg){
super(color,leg);
}
public void eat(){
System.out.println("猫猫爱吃鱼");
}
public void show(){
System.out.println(getColor() + "猫猫" + getLeg() +"腿");
}
}
class Dog extends Animal{
public Dog(){}
public Dog(String color,int leg){
super(color,leg);
}
public void eat(){
System.out.println("狗狗爱吃肉");
}
public void show(){
System.out.println(getColor() + "狗狗" + getLeg() +"腿");
}
}
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2