无法创建继承类的集合

发布于 2024-09-01 09:29:13 字数 1944 浏览 1 评论 0原文

也许我只是不知道要搜索什么,但我在这里有点疯狂,试图弄清楚如何创建继承类的集合。基类,我永远不会使用。

基本上,我有 3 个组件:

  1. 基类调用 ImageFormat
  2. Sub Main() 中ImageForm 代码的子类
  3. 来循环创建一个 收集并循环它。

所以它做到了,#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 类型或单例模式实例化 - 如果您对此有任何想法,我将不胜感激)

然后,我的例程是:

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 增强型图元文件 3 次。我在这里做错了什么?

Maybe I just don't know what to search for, but I'm going a little bonkers here trying to figure out how to create a collection of inherited classes. The base class, I will never use.

Basically, I have 3 components:

  1. A base class call ImageFormat
  2. Child classes of ImageForm
  3. Code in Sub Main() to loop create a
    collection and loop through it.

So it does it, #3. The problem is that it always gets the last item added to the collection and uses it's values only.

Here's my Base Class:

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

And here are the child classes:

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

(don't know if these child classes need to different, like just instantied from ImageFormat type or Singleton patterns - would appreciate anything thoughts you have on this)

Then, my routine is:

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

This returns Windows Enhanced Metafile three times at the Console. What am I doing wrong here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

沙沙粒小 2024-09-08 09:29:13

这三个属性:

Protected Shared _extentions As String()
Protected Shared _targettype As String
Protected Shared _name As String

标记为 Shared - 它们属于类而不是对象。

每次为 _name 分配新值时,它都会覆盖旧值,这就是为什么每次都会打印相同的名称。

应该是:

Protected _extentions As String()
Protected _targettype As String
Protected _name As String

The three properties:

Protected Shared _extentions As String()
Protected Shared _targettype As String
Protected Shared _name As String

Are marked as Shared - they belong to the class not the object.

Each time you assign a new value to _name it overrides the old value, thus why you get the same name printed each time.

It should be:

Protected _extentions As String()
Protected _targettype As String
Protected _name As String
燕归巢 2024-09-08 09:29:13

好吧,您的 _name 等是 Shared,这意味着它们是类级变量。当您添加 WindowsEnhancedMetafile 时,它会用 WMF 特定信息覆盖这些字段。如果您将代码更改为:

imgFormats.Add(New WindowsMetafile)
imgFormats.Add(New WindowsEnhancedMetafile)
imgFormats.Add(New WindowsBitmap)

您将打印“Windows Bitmap”三次。

您所要做的就是将字段声明更改为

Protected _extentions As  String()
Protected _targettype As String
Protected _name As String

Well, your _name et al are Shared, which means they are class-level variables. When you're adding WindowsEnhancedMetafile, it happens to overwrite these fields with WMF-specific information. If you changed your code to:

imgFormats.Add(New WindowsMetafile)
imgFormats.Add(New WindowsEnhancedMetafile)
imgFormats.Add(New WindowsBitmap)

you would've had "Windows Bitmap" printed three times.

All you have to do is to change your field declarations to

Protected _extentions As  String()
Protected _targettype As String
Protected _name As String
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文