class Test1_Animal {
public static void main(String[] args) {
Cat c = new Cat("黑猫",3);
c.eat();
c.sleep();
System.out.println("-----------------");
JumpCat jc = new JumpCat("跳高猫",3);
jc.eat();
jc.sleep();
jc.jump();
System.out.println("-----------------------");
Dog d = new Dog("藏獒",4);
d.eat();
d.sleep();
System.out.println("---------------");
JumpDog jd = new JumpDog("狗跳高" ,12);
jd.eat();
jd.sleep();
jd.jump();
}
}
/*
* A:案例演示
* 动物类:姓名,年龄,吃饭,睡觉。
* 猫和狗
* 动物培训接口:跳高
*/
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();
public abstract void sleep();
}
interface Jumping {
public void jump();
}
class Cat extends Animal {
public Cat(){}
public Cat(String name,int age){
super(name,age);
}
public void eat(){
System.out.println("猫吃鱼");
}
public void sleep(){
System.out.println("侧着睡");
}
}
class JumpCat extends Cat implements Jumping{
public JumpCat(){}
public JumpCat(String name,int age){
super(name,age);
}
public void jump(){
System.out.println("猫跳高");
}
}
class Dog extends Animal {
public Dog(){}
public Dog(String name,int age){
super(name,age);
}
public void eat(){
System.out.println("狗吃肉");
}
public void sleep(){
System.out.println("趴着睡");
}
}
class JumpDog extends Dog implements Jumping {
public JumpDog(){}
public JumpDog(String name,int age){
super(name,age);
}
public void jump(){
System.out.println("狗跳高");
}
} |
|