Java:强制实现接口以扩展抽象类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12953452/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java: Force implementation of interface to extend abstract class
提问by dpelisek
I have several classes implements interface MyInterface and I want all of them to extend abstract class MyAbstractClass. What is the best way to do that?
我有几个类实现了接口 MyInterface,我希望它们都扩展抽象类 MyAbstractClass。最好的方法是什么?
Is there any better way than create another abstract class extending MyAbstractClass and implementing MyInterface?
有没有比创建另一个扩展 MyAbstractClass 并实现 MyInterface 的抽象类更好的方法?
(I swear I haven't found any question like this before I posted)
(我发誓在我发帖之前我没有发现任何这样的问题)
采纳答案by siebz0r
The cleanest solution would be to define such a requirement in the JavaDoc of the interface. In the JavaDoc it should then state something like "to use this interface you need to extend from MyAbstractClass or provide your own implementation". This way the programmer is responsible for the implementation. Remember that this is a sociological problem which you try to solve technicality.
最简洁的解决方案是在接口的 JavaDoc 中定义这样的需求。在 JavaDoc 中,它应该声明诸如“要使用此接口,您需要从 MyAbstractClass 扩展或提供您自己的实现”。通过这种方式,程序员负责实现。请记住,这是一个社会学问题,您试图解决技术问题。
Another 'solution' would be to drop the interface and use an abstract class. Implementing the interface in the abstract class wouldn't make sense here.
另一个“解决方案”是删除接口并使用抽象类。在抽象类中实现接口在这里没有意义。
回答by Jamie
You could define MyAbstractClass
to implement MyInterface
, and then make all of your other classes extend MyAbstractClass
:
您可以定义MyAbstractClass
为实施MyInterface
,然后使所有其他类扩展MyAbstractClass
:
public abstract class MyAbstractClass implements MyInterface {
...
}
public class OneOfSeveral extends MyAbstractClass {
...
}
回答by Anders R. Bystrup
You can't force all your concrete classes to extend MyAbstractClass in any other way than actually changing their definitions from
除了实际更改它们的定义之外,您不能强制所有具体类以任何其他方式扩展 MyAbstractClass
class A implements MyInterface
to
到
class A extends MyAbstractClass
and of course
而且当然
abstract class MyAbstractClass implements MyInterface
You don't need another abstract class as you write, though.
但是,您在编写时不需要另一个抽象类。
Edit: Regarding your comment below: "I want the other way that every implementation of MyInterface extends MyAbstractClass" - you cannot enforce that sensibly. You can define another interface that MyInterface
extends and call that MyAbstractClassInterface
if you want but you still won't be able to enforce your existing classes extend an abstract class implementing this latter interface, although they will of course have to actually implement the methods defined in this interface...
编辑:关于您在下面的评论:“我想要 MyInterface 的每个实现都扩展 MyAbstractClass 的另一种方式”-您无法明智地执行该操作。如果需要,您可以定义另一个MyInterface
扩展和调用的接口,MyAbstractClassInterface
但您仍然无法强制现有类扩展实现后一个接口的抽象类,尽管它们当然必须实际实现此接口中定义的方法...
It sounds to me that you should drop the interface and replace it with the abstract class.
在我看来,您应该删除接口并将其替换为抽象类。
Cheers,
干杯,