集合作为属性的类与继承集合的类 [英] Classes with Collections as Properties vs. Classes Inheriting Collections

查看:217
本文介绍了集合作为属性的类与继承集合的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我使用了一个类继承自一个集合,而不是在类中实例化的集合,这是可以接受的,还是会产生未知的问题进一步下来?以下示例为了清楚起见:

  public class Cars:List< aCar> 

而不是类似:

  public class Cars 
{
List< aCar> CarList = new List< aCar>();任何想法?


h2_lin>解决方案

这个问题是你的Cars类仍然有从List继承的接口,这可能允许你不想要的操作。


Recently I used a class that inherits from a collection instead of having the collection instantiated within the class, is this acceptable or does it create unseen problems further down the road? Examples below for the sake of clarity:

public class Cars : List<aCar>

instead of something like:

public class Cars
{
 List<aCar> CarList = new List<aCar>();
}

Any thoughts?

解决方案

The problem with this is that your Cars class will still have the interface it inherits from List, which may allow operations you don't want.

这篇关于集合作为属性的类与继承集合的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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