继承通用List [英] Inheriting from generic List

查看:74
本文介绍了继承通用List的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为我必须遗漏一些关于泛型的东西,或许只是关于

语法。我想从System.Collections.Generic派生一个MyList类,所以

它只能包含MyItem的实例。


没问题


class MyList< T> :列表< T>

{

}


但这要求我使用MyList< MyItem>创建它myList = new

MyList< MyItem>()我想隐藏列表类中的那一位,所以我可以调用MyList myList = new MyList(
) );


构造函数应该是什么样的?我认为它应该像

非法


类MyList:List< T>

{

public MyList():base(< T>)

{

}

}


,但显然不是!这是可能的还是我对仿制药的理解

完全错了?


(任何人都知道这方面的好基础?)


Andrew

I think I must be missing something about generics, perhaps just about the
syntax. I''d like to derive a class MyList from System.Collections.Generic so
that it can only contain instance of MyItem.

No problem with

class MyList<T> : List<T>
{
}

but that requires me to create it with MyList <MyItem> myList = new
MyList<MyItem>() and I''d like to hide that bit in the list class itself so I
could call MyList myList = new MyList();

What should the constructor look like? I think it should be something like
the illegal

class MyList : List<T>
{
public MyList() : base (<T>)
{
}
}

but it obviously isn''t! Is this possible or is my understanding of generics
completely wrong?

(Anyone know a good primer on this?)

Andrew

推荐答案



" Andrew McLellan" <一个**** @ cix.co.uk> schrieb im Newsbeitrag

新闻:e5 ************* @ TK2MSFTNGP10.phx.gbl ...

"Andrew McLellan" <an****@cix.co.uk> schrieb im Newsbeitrag
news:e5*************@TK2MSFTNGP10.phx.gbl...
我想我一定不能错过关于泛型的东西,也许只是关于
语法。我想从System.Collections.Generic派生一个MyList类,以便它只能包含MyItem的实例。



类没问题MYLIST< T> :列表< T>


但是这需要我用MyList< MyItem>创建它。 myList = new
MyList< MyItem>()我想在列表类本身中隐藏该位,以便我可以调用MyList myList = new MyList();

构造函数应该是什么样的?我认为它应该是类似
非法

类MyList:List< T>
公共MyList():base(< T>)
{
}
}

但显然不是!这是可能的还是我对
泛型完全错误的理解?

(任何人都知道这方面的好基础吗?)

Andrew
I think I must be missing something about generics, perhaps just about the
syntax. I''d like to derive a class MyList from System.Collections.Generic
so that it can only contain instance of MyItem.

No problem with

class MyList<T> : List<T>
{
}

but that requires me to create it with MyList <MyItem> myList = new
MyList<MyItem>() and I''d like to hide that bit in the list class itself so
I could call MyList myList = new MyList();

What should the constructor look like? I think it should be something like
the illegal

class MyList : List<T>
{
public MyList() : base (<T>)
{
}
}

but it obviously isn''t! Is this possible or is my understanding of
generics completely wrong?

(Anyone know a good primer on this?)

Andrew


您可以尝试:

class MyList:List< MyItem>

{

public MyList()

{

}

}


由于MyList总是包含MyItem对象,因此它不是泛型类

本身。


You can try:
class MyList : List<MyItem>
{
public MyList()
{
}
}

Since MyList shall always contain MyItem-objects it''s no generic class
itself.


谢谢,正是我所追求的。


Andrew
Thank you, exactly what I was after.

Andrew


Andrew McLellan< an **** @ cix.co.uk>写道:
Andrew McLellan <an****@cix.co.uk> wrote:
谢谢,正是我所追求的。
Thank you, exactly what I was after.




但在那个阶段,为什么不使用List< MyItem>在你的代码?据我所知,没有必要在这里创建一个新类 - 除非你是否需要增加额外的功能。


-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet 博客: http://www.msmvps.com/jon.skeet

如果回复该群组,请执行不要给我发邮件



But at that stage, why not just use List<MyItem> in your code? There''s
no need to create a new class here, as far as I can see - unless you''re
going to add extra functionality, of course.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


这篇关于继承通用List的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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