黑马程序员技术交流社区

标题: 会跳的猫 [打印本页]

作者: Ayz    时间: 2016-3-21 22:50
标题: 会跳的猫
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("狗跳高");
        }
}




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2