重学java——接口+案例分析 猫和狗jumpping

重学java——接口+案例分析 猫和狗jumpping,第1张

重学java——接口+案例分析 猫和狗jumpping

 

 

package com.cn1;

public class AnimalDemo {
    public static void main(String[] args) {
        jumpping j = new Cat();
        j.jumpping();
        System.out.println("------------------");


        Animal a = new Cat();
        a.setAge(14);
        a.setName("xiu mao~");
        a.eat();
        System.out.println(a.getAge()+".."+a.getName());
        jumpping j1 = new Dog();
        ((Cat) a).jumpping();

        System.out.println("----------------");
        a = new Cat("jiafei",1);
        System.out.println(a.getAge()+".."+a.getName());

        Animal a1 = new Dog();
        a1.setName("xiu gou~~");
        a1.setAge(12);
        a1.eat();
        ((Cat) a1).jumpping();
        System.out.println(a1.getAge()+".."+a1.getName());
        System.out.println("----------------");
        Cat cat = new Cat();
        cat.setAge(13);
        cat.setName("jiajiafeifeifei");
        cat.eat();
        cat.jumpping();
        System.out.println(cat.getAge()+".."+cat.getName());

    }
}

 

package com.cn1;

public abstract class Animal {
    private String name;
    private int age;

    public Animal() {
    }

    public Animal(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
    public abstract void eat();

}
package com.cn1;

public interface jumpping {
    public abstract void jumpping();
}
package com.cn1;

public class Cat extends Animal implements jumpping{
    public Cat() {
    }

    public Cat(String name, int age) {
        super(name, age);
    }

    @Override
    public void eat() {
        System.out.println("cat eat fish");
    }

    @Override
    public void jumpping() {
        System.out.println("jumping catcat");
    }
}
package com.cn1;

public class Dog extends Animal implements jumpping{
    public Dog() {
    }

    public Dog(String name, int age) {
        super(name, age);
    }

    @Override
    public void eat() {
        System.out.println("dog eat meat");
    }

    @Override
    public void jumpping() {
        System.out.println("jumpping dogdog");
    }
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5684188.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存