[英]How do i ensure implementation of a public static function with interface
Since Java 8, we are able to define static and default methods in interface. 从Java 8开始,我们可以在接口中定义静态和默认方法。 But I need to ensure a public static method say foo()
to be implemented in all the classes that implements a particular interface say interface A
. 但是我需要确保在实现特定接口(例如interface A
foo()
所有类中都实现一个公共静态方法foo()
。 How do I do that , or is it at all possible ? 我该怎么做,或者有可能吗?
The interface A: 接口A:
package com.practice.misc.interfacetest;
public interface A {
public static Object foo(); //Eclipse shows error : 'This method requires a body instead of a semicolon'
String normalFunc();
}
Class B : B级:
package com.practice.misc.interfacetest;
public class B implements A{
@Override
public String normalFunc() {
return "B.normalFunc";
}
//I need to ensure that I have to define function foo() too
}
Class C : C级:
package com.practice.misc.interfacetest;
public class C implements A{
@Override
public String normalFunc() {
return "C.normalFunc";
}
//I need to ensure that I have to define function foo() too
}
Edit 1: Actual case : 编辑1:实际情况:
I have one public static method getInstance()
(returning Singleton instance of that class) in all the implementing classes, and I want to ensure all the future classes other developers write must have that static method implemented in their classes. 我在所有实现的类中都有一个公共静态方法getInstance()
(返回该类的Singleton实例),并且我想确保其他开发人员编写的所有将来的类都必须在其类中实现该静态方法。 I can simply use reflection to return that instance by calling the getInstance()
method from a static method of the interface, but I wanted to make sure that everyone implements the getInstance()
in all the implementing classes. 我可以通过从接口的静态方法调用getInstance()
方法来简单地使用反射来返回该实例,但是我想确保每个人都在所有实现类中实现getInstance()
。
static methods from interface are not inherited (1). 接口的静态方法不会被继承(1)。 They are inherited in case of a class, but you can not override them (2); 如果是类,它们是继承的,但是您不能覆盖它们(2); thus what you are trying to do is literally impossible. 因此,您要做的实际上是不可能的。
If you want all classes to implement your method, why not simply make it abstract
(and implicitly public
) to begin with, so that everyone is forced to implement it. 如果你希望所有类来实现你的方法,为什么不干脆让abstract
(和隐含public
)首先,让每个人都被强制执行。
Eugene already pointed out that static methods can not be overridden. 尤金已经指出,静态方法不能被覆盖。 I suggest that you extract the singleton behavior to a separate interface. 我建议您将单例行为提取到单独的接口。 For example: 例如:
public interface A {
String normalFunc();
}
public class B implements A {
@Override
public String normalFunc() {
return "B.normalFunc";
}
// TODO add getInstance for singleton
}
public interface Singleton {
// TODO extensive javadoc to describe expected singleton behavior
A getInstance();
}
public class BSingleton implements Singleton {
@Override
public A getInstance() {
return B.getInstance();
}
}
Finally you can use any object of type BSingleton
to get the singleton object of B
. 最后,您可以使用BSingleton
类型的任何对象来获取B
的单例对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.