class xxx {
public static void main(String[] args) {
Cat c = new Cat();
c.setName("加菲猫");
c.setAge(8);
System.out.println("喵名是:"+c.getName()+"\n"+"喵龄是:"+c.getAge()+"\n");
c.eat();
c.special();
System.out.println("-------");
Cat c1 =new Cat("加菲猫",8);
System.out.println("喵名是:"+c1.getName()+"\n"+"喵龄是:"+c1.getAge()+"\n");
c.eat();
c.special();
}
}
abstract class Animal {
private String name;
private int age;
public Animal(){};
public Animal(String name ,int age){
this.name=name;
this.age =age;
}
public void setName(String name){
this.name=name;
}
public String getName(){
return name;
}
public void setAge(int age){
if (age<0) {
System.out.println("输入年龄有误,请重新输入!");
}else {this.age = age;}
}
public int getAge(){
return age;
}
public abstract void eat();
}
class Cat extends Animal {
public Cat(){}
public Cat(String name,int age){
super(name,age);
}
public void eat(){
System.out.print("猫吃鱼");
}
public void special(){
System.out.println("猫能跳高");
}
}
请问在父类里面有了有参和无参构造子类能继承的,为什么子类里面为什么还要写上??
|
|