简体   繁体   English

如何从抽象类强制实施子类以实现静态方法

[英]How do I enforce a child class from an abstract class to implement a static method

I have the abstract class Animal, and then I have classes like Dog and Cat, wich extend the class Animal. 我有抽象类Animal,然后有Dog和Cat之类,其中扩展了Animal类。

I would like to make sure that all child classes of the parent class Animal have a static method: getProperties. 我想确保父类Animal的所有子类都具有静态方法:getProperties。 Even when someone else, who doesn't have access to my code, implements a new Animal child class. 即使其他人无法访问我的代码,也实现了一个新的Animal子类。

I want it to be static since all Animals of class Dog have the exact same properties (or you don't need a Dog to know how a Dog looks like genericly), and therefor it makes sense that it's a method called on the classtype, rather then a class instance. 我希望它是静态的,因为所有Dog类的Animals都具有完全相同的属性(或者您不需要Dog就能知道Dog的一般外观),因此这是在类类型上调用的方法,而不是一个类实例。

Is this possible? 这可能吗?

A static in Java means that the method (for example) in not related to any of the instances of the class. Java中的static表示该方法(例如)与该类的任何实例都不相关。 It's common to all the instances and it's implementation is just nested within the class. 它对于所有实例都是通用的,并且其实现只是嵌套在类中。

As I read you problem, I believe you need an abstract method in the Animal class. 当我读到您的问题时,我相信您需要Animal类中的abstract方法。

public abstract class Animal {
    public abstract <some-return-type> getProperties();
}

In this case, every class which inherits Animal will have to provide an implementation of the getProperties method (which is not static in this case). 在这种情况下,每个继承Animal类都必须提供getProperties方法的实现(在这种情况下,该方法不是 static )。

If you want it to be static, just make it so (within the Animal class), but then you will have a single and shared implementation. 如果你希望它是静态的,只要不是这样(在中Animal类),但随后你将有一个单一的共享的实现。

No, not possible. 不,不可能。 There's no inheritance in static contexts. 在静态上下文中没有继承。 And no, there's no way to enforce a class to implement a static method. 不,没有方法可以强制实现静态方法的类。

The Java Object model doesn't support class-side inheritance (unlike Smalltalk or Ruby). Java Object模型不支持类端继承(与Smalltalk或Ruby不同)。 You have to explicitly implement a meta model of your domain, for instance, like this: 例如,您必须显式实现域的元模型,如下所示:

public abstract class AnimalDescriptor<T> { // or <T extends Animal>

    public abstract List<String> getProperties();

    public abstract T newAnimal();

}

public class DogDescriptor extends AnimalDescriptor<Dog> {

    public static final DogDescriptor INSTANCE = new DogDescriptor();

    public List<String> getProperties() { ... }

    public Dog newAnimal() { return new Dog(); }

}

void doStuff(AnimalDescriptor<?> desc) {
    desc.getProperties();
}

doStuff(DogDescriptor.INSTANCE);

This is just an example, you will have to adapt it to suit your needs. 这只是一个示例,您将不得不对其进行调整以适合您的需求。 For instance, you might want to add a getDescriptor() method on the animal side. 例如,您可能想在动物方面添加getDescriptor()方法。

在Java中这是不可能的,您最多可以“隐藏”静态方法,但不能“覆盖”静态方法。

If you want to have each Animal class provide metadata about it, give it instance methods like getProperties , getNumberOfLegs , getLocomotionType , etc. You can implement these using static constants: 如果要让每个Animal类提供有关它的元数据,请给它提供实例方法,如getPropertiesgetNumberOfLegsgetLocomotionType等。您可以使用静态常量来实现这些方法:

public abstract class Animal {
    public abstract int getNumberOfLegs();
}

public class Dog {
    private static final NUMBER_OF_LEGS = 4;
    public int getNumberOfLegs() {
        return NUMBER_OF_LEGS;
    }
    // ...
}

I suppose you wanted a Map of some sort for the getProperties method. 我想您想要getProperties方法的某种Map The problem with that is that you must be very careful to make the map you return immutable. 这样做的问题是您必须非常小心,以使返回的地图不可变。 One mistake, and someone can change the static instance for everyone. 一个错误,有人可以为每个人更改静态实例。 making the method an instance method costs you almost nothing. 使该方法成为实例方法几乎不会花费您什么。

Static methods need to be able to run without you explicitly creating a new instance of the Animal class. 静态方法需要能够运行,而无需您显式创建Animal类的新实例。 You will have to fully implement Animal.getProperties() . 您将必须完全实现Animal.getProperties()

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

相关问题 如何从抽象类强制执行方法? - How to enforce method execution from abstract class? static 方法来自摘要 class - static method from abstract class 在派生类中将抽象方法实现为静态 - implement an abstract method in derived class as static 使用子对象数组时,如何调用抽象类的子类方法? - How do I call abstract class' child class method when using an array of child objects? 如何约束类以实现特定的静态方法? - How do I constrain a class to implement a particular static method? 如何使用EasyMock模拟从抽象类继承的方法? - How do I mock a method inherited from an abstract class with EasyMock? 如何从Class对象运行抽象方法来扩展Java中的抽象类? - How do I run an abstract method from Class object extending abstract class in Java? 如何从另一个类的静态方法获取抽象类的子类的实例? - How can I get an instance of a subclass of abstract class from a static method of another class? 如何强制实现接口或扩展抽象类的子类的类必须提供默认构造函数? - How do I enforce that a class implementing an interface or a subclass extending an abstract class must provide a default constructor? Java子类无法实现父类的抽象方法 - Java child class not able to implement parent class's abstract method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM