问题描述
有没有办法 NOT 实现继承类中接口的所有方法?
Is there any way to NOT implement all of the methods of an interface in an inheriting class?
推荐答案
解决这个问题的唯一方法是将您的类声明为 abstract
,并将其保留为子类以实现缺少的方法。但最终,链中的某个人必须实现它以满足接口合同。如果你真的不需要特定的方法,你可以实现它,然后返回
或抛出一些 NotImplementedException
,在你的情况下哪个更合适。
The only way around this is to declare your class as abstract
and leave it to a subclass to implement the missing methods. But ultimately, someone in the chain has to implement it to meet the interface contract. If you truly do not need a particular method, you can implement it and then either return
or throw some variety of NotImplementedException
, whichever is more appropriate in your case.
接口也可以指定一些方法为'default'并在接口定义中提供相应的方法实现()。实现接口时无需提及这些默认方法。
The Interface could also specify some methods as 'default' and provide the corresponding method implementation within the Interface definition (https://docs.oracle.com/javase/tutorial/java/IandI/defaultmethods.html). These 'default' methods need not be mentioned while implementing the Interface.
这篇关于没有实现接口的所有方法。可能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!