也许我只是不知道要搜索什么,但是我在这里试着弄清楚如何创建一个继承类的集合。基类,我永远不会使用。无法创建继承类的集合
基本上,我有3个分量:通过它创建一个 收集和循环
- 甲基类呼叫的imageformat ImageForm
- 代码中的Sub Main()的
- 子类来循环。
所以它这样做,#3。问题是它总是获取最后一个项目添加到集合,并只使用它的值。
这里是我的基类:
Public MustInherit Class ImageFormat
Protected Shared _extentions As String()
Protected Shared _targettype As String
Protected Shared _name As String
Public ReadOnly Property Extentions As String()
Get
Return _extentions
End Get
End Property
Public ReadOnly Property TargetType As String
Get
Return _targettype
End Get
End Property
Public ReadOnly Property Name As String
Get
Return _name
End Get
End Property
End Class
这里是子类:
Class WindowsEnhancedMetafile
Inherits ImageFormat
Sub New()
_extentions = {"EMF"}
_targettype = "jpg"
_name = "Windows Enhanced Metafile"
End Sub
End Class
Class WindowsBitmap
Inherits ImageFormat
Sub New()
_extentions = {"BMP", "DIB", "RLE", "BMZ"}
_targettype = "jpg"
_name = "Windows Bitmap"
End Sub
End Class
Class WindowsMetafile
Inherits ImageFormat
Sub New()
_extentions = {"WMF"}
_targettype = "jpg"
_name = "Windows Metafile"
End Sub
End Class
(不知道这些子类需要不同的,像刚从的imageformat型或instantied单身模式 - 将不胜感激您对此有任何想法)
然后,我的例程是:
Sub Main()
Dim imgFormats As New List(Of ImageFormat)
imgFormats.Add(New WindowsBitmap)
imgFormats.Add(New WindowsMetafile)
imgFormats.Add(New WindowsEnhancedMetafile)
Dim name As String = String.Empty
For Each imgFormat In imgFormats
name = imgFormat.Name
Console.WriteLine(name)
Next
Console.ReadLine()
End Sub
这将返回Windows增强图元文件三次在控制台。我在这里做错了什么?
完美!谢谢! – 2010-05-12 07:04:31