更新时间:2022-01-29 22:50:34
解决这个问题的唯一方法是将您的类声明为 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'并在接口定义中提供相应的方法实现( 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.