class Demo_Animal {
public static void main(String[] args) {
Cat c = new Cat("小花",8);
System.out.println(c.getName()+","+c.getAge());
c.eat();
c.catchMouse();
c.jump();
Dog d = new Dog("黑子",4);
System.out.println(d.getName()+","+d.getAge());
d.eat();
d.lookHome();
}
}
/*1
猫狗案例,加入跳高的额外功能
* 具体事物:猫,狗
* 共性:姓名,年龄,吃饭
* 猫的特性:抓老鼠
* 狗的特性:看家
*/
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 {
public Cat(){}
public Cat(String name,int age){
super(name,age);
}
public void eat(){
System.out.println("猫吃鱼");
}
public void catchMouse(){
System.out.println("抓老鼠");
}
public void jump(){
System.out.println("猫跳高");
}
}
class Dog {
public Dog(){}
public Dog(String name,int age){
super(name,age);
}
public void eat(){
System.out.println("狗吃肉");
}
public void lookHome(){
System.out.println("看家");
}
} |
|