标题: 小猫和小狗案例 [打印本页] 作者: 丶不缺小哥范儿 时间: 2015-9-17 17:22 标题: 小猫和小狗案例 class Test1_Animal {
public static void main(String[] args) {
System.out.println("Hello World!");
Dog d = new Dog("哮天犬",30);
System.out.println(d.getName()+"..."+d.getAge());
d.eat();
d.lookHome();
Cat c = new Cat("加菲猫",3);
System.out.println(c.getName()+"..."+c.getAge());
c.eat();
c.catchMouse();
}
}
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){
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.println("猫吃鱼");
}
public void catchMouse() {
System.out.println("抓老鼠");
}
}