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

问题描述

我的问题是,如果通过扩展已经实现它的类隐式实现的接口应该由该类显式实现,如果该类想要宣传它履行该接口的契约的事实.

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 的约定.你实现这个,扩展类java.util.AbstractList,它已经实现了接口List.您是否明确声明,您实现了 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.

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

06-27 18:41