Java Case 8: Breeder feeding animals

Idea:

Keeper feeding animals
Feed different foods to different animals, and the animals will make different noises each time they are fed
Write a program to simulate the process of feeding animals by a breeder
Feed bones to the puppy and the puppy barks
Feed the little fish to the kitten, and the kitten meows

Abstract three classes:
breeder feeder
animals
food food

Assuming that only cats and dogs are considered, the Cat class and Dog class are derived from the animal class
Similarly, the food interface can further derive Bone, Fish,
Because there is an obvious logical relationship of is a between them.

Code:

Code structure:

Test class:

package base.base008;

/*
饲养员喂养动物
给不同的动物喂不同的食物,且在每次喂食时,动物会发出不同的叫声
编写程序模拟饲养员喂食动物的过程
给小狗喂骨头,小狗汪汪叫
给小猫喂小鱼,小猫喵喵叫

抽象出三个类:
饲养员 feeder
动物 animal
食物 food

假设只考虑猫类和狗类,则由animal类派生出Cat类和Dog类
同理,food接口可以进一步派生出Bone、Fish,
因为他们之间存在着明显的 is a 的逻辑关系。

 */

public class Test08 {
    public static void main(String[] args) {
        Feeder f1 = new Feeder("夏习清");

        f1.speak();

        Dog dog = new Dog("二哈");
        dog.shout();
        dog.eat(new Bone());
        //f1.feeder(dog,new Bone());

        Cat cat = new Cat("狸花");
        cat.shout();
        cat.eat(new Fish());
        //f1.feeder(cat,new Fish());
    }
}

Animal class:

package base.base008;

public abstract class Animal {
    private String name;

    public Animal() {
    }

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

    public String getName() {
        return name;
    }

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

    //动物叫的方法
    public abstract void shout();
    public abstract void eat(Food food);

}

Breeder feeder class:

package base.base008;

public class Feeder {
    private String name;

    public Feeder() {
    }

    public Feeder(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

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

    //介绍自己信息
    public void speak(){
        System.out.println("欢迎来到动物园!");
        System.out.println("我是饲养员:"+getName());
    }

    //饲养方法
    public void feeder(Animal a,Food food){
        a.eat(food);

    }


}

Food interface:

package base.base008;

public interface Food {
    public abstract String getName();
}

Bone class:

package base.base008;

public class Bone implements Food{

    @Override
    public String getName() {
        return "骨头";
    }
}

Fish category:

package base.base008;

public class Fish implements Food{

    @Override
    public String getName() {

        return "小鱼";
    }
}

Cat class:

package base.base008;

public class Cat extends Animal {

    public Cat() {
    }

    public Cat(String name) {
        super(name);
    }

    @Override
    public void shout() {
        System.out.print("喵喵喵~");

    }

    @Override
    public void eat(Food food) {
        System.out.println(getName()+"正在吃"+food.getName());

    }
}

Dog class:

package base.base008;

public class Dog extends Animal {

    public Dog() {
    }

    public Dog(String name) {
        super(name);
    }

    @Override
    public void shout() {
        System.out.print("汪汪汪~");

    }

    @Override
    public void eat(Food food) {
        System.out.println(getName()+"正在吃"+food.getName());

    }
}

Guess you like

Origin blog.csdn.net/weixin_54446335/article/details/131169121