未调用阴影方法

发布于 2024-10-09 15:43:21 字数 1603 浏览 3 评论 0原文

我有一个类,我意识到它并不总是正确实例化,作为快速修复,我想我应该对它进行子类化并隐藏一些方法,以便程序可以继续运行而不会爆炸。当我运行软件时,对方法的调用解析为基础的实现而不是子类。我正在使用 VB.NET 和 .NET 2.0。这是我想要做的一个示例:

Public Class SuperClass

  Public Sub New ()
    Dim type As Type = GetType(SubClass)
    If (Me.GetType() is type) Then
      //nothing
    Else
      //build real object
    EndIf
  End Sub

  Private Shared _Instance As SuperClass
  Public Shared ReadOnly Property Instance() As SuperClass
    Get
      If (_Instance Is Nothing) Then
        Try
          _Instance = New SuperClass()
        Catch ex As Exception
          Dim result As DialogResult = MessageBox.Show(text, caption, MessageBoxButtons.RetryCancel, MessageBoxIcon.Information)
          If (result = DialogResult.Retry) Then
            _Instance = New SuperClass()
            //this will probably cause problems of its own, but i'll cross that bridge later...
          Else
            _Instance = New SubClass()
          End If
        End Try

      End If
      Return _Instance
    End Get
  End Property

  Public Overridable Function MyFunction() As Integer
     Dim somethingReasonable As Integer //do something for real
     Return somethingReasonable
  End Function

End Class

Public Class SubClass
  Inherits SuperClass

  Public Sub New()
    //doesn't do what cause the exception in the first place
  End Sub

  Public Shadows Function MyFunction() As Integer
    //Do something safe
    Return -1
  End Function    

End Class

我不确定为什么在运行时调用基类。当我在调试器中检查对象时,它显然是子类类型,但调用了超类方法。对对象的访问是通过实例属性进行的。

我确信我做错了什么或做出了错误的假设,但我不知道是什么。

谢谢, 布莱恩

I have a class which i realized will not always correctly instantiate and as a quick fix, i figured i'd subclass it and shadow a few methods so that the program can continue to run without exploding. When i run the software, calls to the methods resolve to the base's implementations and not the subclass. I'm using VB.NET with .NET 2.0. Here is an example of what i'm trying to do:

Public Class SuperClass

  Public Sub New ()
    Dim type As Type = GetType(SubClass)
    If (Me.GetType() is type) Then
      //nothing
    Else
      //build real object
    EndIf
  End Sub

  Private Shared _Instance As SuperClass
  Public Shared ReadOnly Property Instance() As SuperClass
    Get
      If (_Instance Is Nothing) Then
        Try
          _Instance = New SuperClass()
        Catch ex As Exception
          Dim result As DialogResult = MessageBox.Show(text, caption, MessageBoxButtons.RetryCancel, MessageBoxIcon.Information)
          If (result = DialogResult.Retry) Then
            _Instance = New SuperClass()
            //this will probably cause problems of its own, but i'll cross that bridge later...
          Else
            _Instance = New SubClass()
          End If
        End Try

      End If
      Return _Instance
    End Get
  End Property

  Public Overridable Function MyFunction() As Integer
     Dim somethingReasonable As Integer //do something for real
     Return somethingReasonable
  End Function

End Class

Public Class SubClass
  Inherits SuperClass

  Public Sub New()
    //doesn't do what cause the exception in the first place
  End Sub

  Public Shadows Function MyFunction() As Integer
    //Do something safe
    Return -1
  End Function    

End Class

I'm not sure why the base class gets called during runtime. When i inspect the object in the debugger it is clearly the SubClass type but the SuperClass methods get called. Access to the object is through the Instance Property.

I'm sure i'm doing something wrong or making some wrong assumption but I can't figure out what.

Thanks,
brian

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

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

发布评论

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

评论(1

ら栖息 2024-10-16 15:43:21

如果一个方法被隐藏而不是被覆盖,那么当实例是子类类型时,将不会调用隐藏方法——将调用的方法是根据接收者的编译时类型而不是运行时类型确定的。这是隐藏和覆盖之间的根本区别。

If a method is shadowed rather than overridden, the shadowing methods will not be called when an instance is of subclass type -- the method which will be called is determined based on the compile-time type of the receiver rather than the run-time type. This is the fundamental difference between shadowing and overriding.

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