在我的自定义集合中使用另一个集合?

huangapple go评论77阅读模式
英文:

Use a Collection inside my custom collection?

问题

这是翻译好的内容:

使用已经实现的集合(比如 ArrayList)来实现自定义集合,是否正确?或者可能会有问题?

类似这样:

public class customCollection<E> implements Collection<E> {
    List<E> objects = new ArrayList<E>();
}
英文:

Is it correct to use an already implemented Collection (like a ArrayList) to implement my custom collection? Or could be there any problem?

Something like this:

public class customCollection&lt;E&gt; implements Collection &lt;E&gt; {
    List&lt;E&gt; objects = new ArrayList&lt;E&gt;();
}

答案1

得分: 2

这完全没问题。
我有一些专门的数据类(还具有一些业务逻辑),它们实现了一种标准类型,并在内部为其他原因使用了各种对象。

要注意不要产生难以维护的代码。为此,你可以使用诸如SonarQube之类的工具。检查一下,当你依赖(许多)类时,你使用了多少 - 换句话说,你的类对其他接口、类、继承方法有多依赖?参考,例如http://tutorials.jenkov.com/ood/understanding-dependencies.html

英文:

That is absolutely OK.
I have specialised data classes (that also have some business logic) that implement one standard type and have internally various objects for other reasons.

Take care to not produce unmaintainable code. For this, you could use tools such as SonarQube. Check, when you rely on (many) classes, how much do you use - in other words, how dependent is your class froom other interfaces, classes, inherited methods? See, for example http://tutorials.jenkov.com/ood/understanding-dependencies.html

huangapple
  • 本文由 发表于 2020年5月4日 01:51:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/61579116.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定