是否应该在子类中显式实现从基类继承的接口? [英] Should an interface that is inherited from base-class be implemented explicitly in subclass?

查看:116
本文介绍了是否应该在子类中显式实现从基类继承的接口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是,如果通过扩展已经实现它的类来隐式实现的接口应该由类显式实现,如果类想要宣传这个事实,那么它就完成了该接口的契约。

My question is, if an interface that is implemented implicitly by extending a class that already implements it, should be explicitly implemented by the class, if the class wants to advertise the fact, that it fulfills the contract of that interface.

例如,如果你想写一个类,那就完成了接口的合约 java.util.List 。实现它,扩展已经实现接口 List 的类 java.util.AbstractList 。你是否明确声明你实现了List?

For instance, if you want to write a class, that fulfills the contract of the interface java.util.List. You implement this, extending the class java.util.AbstractList, that already implements the interface List. Do you explicitly declare, that you implement List?

public class MyList extends AbstractList implements List

或者您是否使用隐式方式保存输入?

Or do you save typing by using the implicit way?

public class MyList extends AbstractList

哪种方式被认为是更好的风格?你有什么理由喜欢这种或那种方式?在哪种情况下你更喜欢方式1或方式2?

Which way is considered better style? What reasons do you have to prefer one way or another? In which situations you would prefer way 1 or way 2?

推荐答案

我问很久以前在我的博客上提出同样的问题。如果你有兴趣看到其他人的想法,那里也会有很长的讨论。值得注意的是,这两种策略都是在JDK中进行的。

I asked this same question long ago on my blog. There is a long discussion there as well if you're interested in seeing some other people's thoughts. It's interesting to note that both strategies are taken within the JDK.

我最终决定对此采取硬性规则没有意义 - 最好根据我想要沟通的内容做出最佳判断。

I ultimately decided that a hard rule on this didn't make sense - it's better to use best judgement as to what I wanted to communicate.

这篇关于是否应该在子类中显式实现从基类继承的接口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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