扩展泛型列表 [英] Extend a Generic.List
问题描述
我需要创建几种类型的对象的列表.
每个列表对象都有一些共同点,所以我想创建一个像这样的基列表类...
I have a need to create Lists of several types of objects.
Each list object will have some things in common, so I thought create a base list class like so...
Public MustInherit Class BaseList
Inherits List
Private _oParent as Object
Public Sub New(ByVal parent as Object)
MyBase.New
_oParent = parent
End Sub
然后像这样消耗BaseList对象..
And the consume the BaseList object like so..
Public Class People
Inherits BaseList(Of Person)
End Class
继承列表中不喜欢的是BaseList对象,因为它需要一个类型.
如何解决此问题.
谢谢
What is not liked in the Inherits List is the BaseList object as it expects a Type.
How do I resolve this issue.
Thanks
推荐答案
史蒂芬
看看这个简单的例子,我认为它应该给你一个想法
Steven
have a look at this simple example I think it should give you an idea
Public Class Person
Private _Name As String = String.Empty
Public Property Name() As String
Get
Return _Name
End Get
Set(ByVal value As String)
_Name = value
End Set
End Property
End Class
Public Class Emplyoee
Inherits Person
Private _EmpID As Integer = 0
Public Property EmployeeID() As Integer
Get
Return _EmpID
End Get
Set(ByVal value As Integer)
_EmpId = value
End Set
End Property
End Class
然后这将是您用来创建genric.list(of t)的变量,即dim People as new list(of Employee)
.
这也可能是一篇不错的文章,它帮助我获得了继承的基础知识如何在Visual Basic 2005或Visual Basic中使用继承. NET [ ^ ]
This would then be the variable you use to create the genric.list(of t) i.e. dim People as new list(of Employee)
.
This might also prove to be a good article it helped me get the basics of inheritance How to use inheritance in Visual Basic 2005 or in Visual Basic .NET[^]
它看起来就像你对OOP一无所知.这称为多态性;并且应该子类化不是容器类型(列表),而是元素类型(人),并使用接口或 late绑定. />
您的代码已无法解决.逐步学习有关语言和.NET的书籍或手册,然后重新开始.在路上,请注意:
http://en.wikipedia.org/wiki/Oop [ http://en.wikipedia.org/wiki/Late_binding [ http://en.wikipedia.org/wiki/Dynamic_dispatch [ http://en.wikipedia.org/wiki/Polymorphism_%28computer_science%29 [ ^ ],
http://en.wikipedia.org/wiki/Interface_%28object-oriented_programming%29 [ ^ ].
—SA
It looks like you have no idea of OOP. This is called polymorphism; and you should subclass not the container type (list), but the element type (person) and use either interfaces or late binding.
You code is beyond the fix. Through it out, take a book or a manual on language and .NET and start over. Down the road, pay attention at:
http://en.wikipedia.org/wiki/Oop[^],
http://en.wikipedia.org/wiki/Late_binding[^],
http://en.wikipedia.org/wiki/Dynamic_dispatch[^],
http://en.wikipedia.org/wiki/Polymorphism_%28computer_science%29[^],
http://en.wikipedia.org/wiki/Interface_%28object-oriented_programming%29[^].
—SA
这篇关于扩展泛型列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!