简体   繁体   English

实例化子类中的变量

[英]Instantiate a variable in a child class

is possible to instantiate a variable in a child class that was declared in the parent class?是否可以在父类中声明的子类中实例化变量? What would be the advantage?会有什么好处? Ex:前任:

public class Animal{ Food foodType;}
public class Dog extends Animal{ 
   public Dog(){
       foodType=new Food();
   }
}

There is a lot of advantage of doing it.这样做有很多好处。 Actually depends on your design.实际上取决于您的设计。

I created an example, maybe the worse example that I could, but I think it will make you clarify your mind.我创造了一个例子,也许是我能做的最糟糕的例子,但我认为它会让你澄清你的想法。 I just tried to follow your code.我只是试着按照你的代码。

In this example we use Strategy design pattern and Inversion of Control .在这个例子中,我们使用Strategy设计模式和Inversion of Control You can see that Animal doesn't know nothing about Food implementation?你可以看到 Animal 对 Food 的实现一无所知? Think about it, Animal.eat() can run multiple implementations without changing the eat() method.想想看, Animal.eat()可以在不改变eat()方法的情况下运行多个实现。 It is a little bit what the OOP can do. OOP 可以做的就是一点点。

public class Main {
    public static void main(String ... args){
        Animal paul = new Dog("Paul");
        Animal elsa = new Cat("Elsa");

        paul.eat();
        elsa.eat();
    }

    public static abstract class Animal {
        private String name;
        protected Food foodType;

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

        public void eat() {
            System.out.println("The " + name + " has eaten " + foodType.getAmount() + " " + foodType.foodName());
        }
    }

    public static class Dog extends Animal {
        public Dog(String name) {
            super(name);
            foodType = new DogFood();
        }
    }

    public static class Cat extends Animal {
        public Cat(String name) {
            super(name);
            foodType = new CatFood();
        }
    }

    public interface Food {
        int getAmount();
        String foodName();
    }

    public static class DogFood implements Food{
        @Override
        public int getAmount() {
            return 2;
        }

        @Override
        public String foodName() {
            return "beef";
        }
    }

    public static class CatFood implements Food{
        @Override
        public int getAmount() {
            return 5;
        }

        @Override
        public String foodName() {
            return "fish";
        }
    }
}

advantage:优势:

For ex.例如。
If there is DogFood class extends Food, Dog can instantiate it (if Dog knows about DogFood) and Animal does not need to know about DogFood.如果有 DogFood 类扩展 Food,Dog 可以实例化它(如果 Dog 知道 DogFood)并且 Animal 不需要知道 DogFood。

This simplifies codes.这简化了代码。

As food type is an Animal property, and always the same for a specific child class, make it final in Animal, and pass it in via the Animal constructor.由于 food 类型是一个 Animal 属性,并且对于特定的子类始终相同,因此在 Animal 中将其final ,并通过 Animal 构造函数传入。

public class Animal {
    public final Food foodType;

    public Animal(Food foodType) {
        this.foodType = foodType;
    }
}

public class Dog extends Animal{ 
    public Dog() {
       super(new DogFood());
    }
}

Now every Animal (child) has a foodType, and one does not have to fill the field of a parent class.现在每个 Animal(子类)都有一个 foodType,并且不必填充父类的字段。 The responsibilities are where they should be.责任就在他们应该在的地方。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM