没有实现接口的所有方法.是否可以? [英] not implementing all of the methods of interface. is it possible?
问题描述
有没有办法不在继承类中实现接口的所有方法?
Is there any way to NOT implement all of the methods of an interface in an inheriting class?
推荐答案
解决此问题的唯一方法是将您的类声明为 abstract
并将其留给子类来实现缺少的方法.但最终,链中的某个人必须实现它才能满足接口契约.如果您确实不需要特定的方法,则可以实现它,然后return
或抛出一些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.
接口还可以将某些方法指定为默认"并在接口定义中提供相应的方法实现 (https://docs.oracle.com/javase/tutorial/java/IandI/defaultmethods.html).在实现接口时不需要提及这些默认"方法.
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.
这篇关于没有实现接口的所有方法.是否可以?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!