加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 泛型或非泛型

发布时间:2020-12-15 18:36:09 所属栏目:百科 来源:网络整理
导读:基本上我有一个包含不同水果的自定义List类.假设每个水果都有一个存储在列表中的ID号. 有没有更好的: new AppleList();new OrangeList();new LemonList(); 要么 new FruitListFruit.Apple();new FruitListFruit.Orange();new FruitListFruit.Lemon(); 需要
基本上我有一个包含不同水果的自定义List类.假设每个水果都有一个存储在列表中的ID号.

有没有更好的:

new AppleList();
new OrangeList();
new LemonList();

要么

new FruitList<Fruit.Apple>();
new FruitList<Fruit.Orange>();
new FruitList<Fruit.Lemon>();

需要考虑的事项:

>所有ID都是int类型.
>水果的类型不会影响List本身的实现.它只会被列表的客户端使用,就像外部方法一样.

我想使用更清晰,更好的设计,更快,更高效等的那个.此外,如果以上2种技术不是最好的,请提出您的想法.

编辑:如果不清楚,顺便说一句水果是一个枚举.

解决方法

?All IDs are of type int.

?The type of the fruit will not affect the implementation of the List itself. It will only be used by the client of the list,like an external method,etc.

鉴于这两个事实,我不会打扰泛型.我会在FruitList上放一个普通的属性来指示它是哪种类型的水果.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读