class Day_Animal {
public static void main(String[] args) {
Cat c = new Cat();
c.setColor("花");
c.setLeg(4);
System.out.println(c.getColor() + "..." + c.getLeg());
c.eat();
c.function();
System.out.println("--------------------------");
Dog d = new Dog("黄",4);
System.out.println(d.getColor() + "..." + d.getLeg());
d.eat();
d.function();
}
}
class Animal {
private String color;
private int leg;
//无参
public Animal (){}
//有参
public Animal (String color,int leg){
this.color = color;
this.leg = leg;
}
//设置set
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("吃肉");
}
public void function(){
System.out.println("抓老鼠");
}
}
class Cat extends Animal {
public Cat (){}
//隐藏着一个 super();
public Cat (String color,int leg){
//如果这是里没有写super();那么自动认为super();指定父类无参。
}
//重写父类的eat方法
public void eat (){
System.out.println("吃鱼");
}
}
class Dog extends Animal{
public Dog () {}
//隐藏着一个 super();
public Dog (String color,int leg) {
super (color,leg);
}
//重写父类的function
public void function () {
System.out.println("看门");
}
}
|
|