简体   繁体   English

java静态方法和接口/继承

[英]java static methods and interfaces/inheritance

Ok, so I have a hierarchy of classes and subclasses. 好的,所以我有一个类和子类的层次结构。 They all must have certain methods. 它们都必须具有某些方法。 Those methods are in an interface implemented by the superclass and everything below. 这些方法位于超类及其下面所有内容实现的接口中。

There is one method they should all have and define but that method is "in spirit", of the static kind, returning the same results for all instances of a given class. 他们都应该拥有并定义一个方法,但是该方法是“本质上”的静态类型,对于给定类的所有实例返回相同的结果。

Unfortunately, one cannot have a static method as part of an interface and also, static method do not support inheritance, they "hide" rather than "override". 不幸的是,不能将静态方法作为接口的一部分,而且,静态方法不支持继承,它们“隐藏”而不是“覆盖”。

So, what are the options for a static method that all classes in a hierarchy must have and that can be called in a generic way? 那么,层次结构中所有类都必须具有的静态方法的选项是什么,并且可以通过通用方式调用这些方法? At the moment, I made that method non static... 目前,我将该方法设为非静态...

Also, ideally, that shouldn't involve having a second hierarchy of classes that must be kept in sync just for that one method... 同样,理想情况下,这不应该包含仅针对该一种方法就必须保持同步的第二个类层次结构...

Many Thanks 非常感谢

====== ======

EDIT: People asked for an example so here you go: 编辑:人们问一个例子,所以在这里你去:

OK, let's say i have a hierarchy of animal classes (abstract Animal class > abstract Mammal class > Cat class, Dog class, Dolphin class, etc). 好的,假设我有一个动物类的层次结构(抽象动物类>抽象哺乳动物类>猫类,狗类,海豚类等)。 Some of the behaviour will be instance specific, they may have members like "weight", "name", "age", etc and have related getter/setter. 一些行为将是特定于实例的,它们可能具有“ weight”,“ name”,“ age”等成员,并具有相关的getter / setter。

but then, there might be one method that would typically give the same result for all the instances of a given class, for example it could be one of those: 但是,可能会有一个方法通常会为给定类的所有实例提供相同的结果,例如,它可能是以下一种:

-int getNumberOfLegs();
-String getFavouriteFood();

All cats have 4 legs so that method could be static, it's not instance-dependant. 所有的猫都有4条腿,因此该方法可以是静态的,而不依赖于实例。 it's more like meta information. 它更像是元信息。 I want to be able to do Cat.getNumberOfLegs(). 我希望能够执行Cat.getNumberOfLegs()。 I want to force the existence of that method on all my hierarchy in a way that can be overridden, etc. 我想以一种可以覆盖的方式,在我的所有层次结构中强制该方法的存在。

Though it shall be better if you can support your question with example. 如果可以通过示例支持您的问题,虽然会更好。 From what I understood is, you want subclasses to write their own behavior for certain method - thus you need overriding. 据我了解,您希望子类为某些方法编写自己的行为-因此您需要重写。 Thus probably this method is not a candidate for being static. 因此,该方法可能不是静态的候选方法。 Probably what best you can do is, to have an abstract class. 可能最好的办法就是拥有一个抽象类。 Provide a default implementation, if you really insist. 如果您真的坚持的话,请提供默认实现。 Or Just make sure that every subclass defines it. 或者只是确保每个子类都定义了它。 For that make the method it self as abstract. 为此,使该方法本身具有抽象性。

After EDIT: Quoting you: 编辑后:引用您:

I want to force the existence of that method on all my hierarchy in a way that can be overridden, etc. 我想以一种可以覆盖的方式,在我的所有层次结构中强制该方法的存在。

Above is clearly indication that you need abstract. 以上清楚表明您需要抽象。 Not Static. 不是静态的。

-int getNumberOfLegs();
-String getFavouriteFood();

Shall be different for dolphin and dog. 海豚和狗应有所不同。 So there is no way you can do it through static method. 因此,您不可能通过静态方法来做到这一点。 Static method can be for your mammel class where it can have something like 静态方法可以用于您的Mammel类,其中可以包含以下内容

String getMyNature {
    // return Mammel nature
}

Static has to be class level. 静态必须是类级别的。 But since you want the same interface to access instance specific information, you shall have to use abstract (for forcing each implementation to have it). 但是,由于您希望同一个接口访问实例特定的信息,因此您必须使用抽象(强制每个实现都必须具有抽象)。

Apart of an abstract method in your Animal that would be overriden in each class to provide a constant value like Animal中的抽象方法的一部分,在每个类中将被覆盖以提供恒定值,例如

class Cat extends Animal {
    private static final int NUMBER_OF_LEGS_ON_A_CAT = 4;

    @Override
    public int getNumberOfLegs() {
        return NUMBER_OF_LEGS_ON_A_CAT;
    }
}

you can also create non-abstract method in your superclass and provide the constant through super constructor like this: 您还可以在您的超类中创建非抽象方法,并通过超级构造函数提供常量,如下所示:

class Animal {
    private final int numberOfLegs;

    protected Animal(int numberOfLegs) {
        this.numberOfLegs = numberOfLegs;
    }        

    public final int getNumberOfLegs() {
        return numberOfLegs;
    }
}

class Cat extends Animal {
    private static final int NUMBER_OF_LEGS_ON_A_CAT = 4;

    public Cat() {
        super(NUMBER_OF_LEGS_ON_A_CAT);
    }
}

This approach may be less verbose, on the other hand can become quite unreadable if there's too many constants like this. 这种方法可能不太冗长,另一方面,如果这样的常量太多,则可能变得非常难以理解。


The bottom line is however that static methods and subtyping don't go well together, so the best you can do is probably make the method non-static and ensure that the data it returns is static - this is achieved by providing a constant through constructor into a final field, by returning a constant in the overriden method, etc. 但最重要的是,静态方法和子类型不能很好地结合在一起,因此,您可以做的最好的事情就是使该方法成为非静态方法,并确保它返回的数据是静态的-这是通过构造函数提供一个常量来实现的通过在重写方法中返回常量等,将其转换为最终字段

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

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