没有实现接口的所有方法。可能吗? [英] not implementing all of the methods of interface. is it possible?

查看:255
本文介绍了没有实现接口的所有方法。可能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法 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'并在接口定义中提供相应的方法实现( 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆