更新:2007 年 11 月
提供一种引用当前类实例成员的方法,而在引用之前无需通过任何派生类重写来替换当前类实例成员。
备注
MyClass 关键字的行为与最初实现时引用类的当前实例的对象变量的行为相似。MyClass 类似于 Me,但在调用 MyClass 中的每个方法和属性时,可将此方法或属性当作 NotOverridable 中的方法或属性对待。因此,方法或属性不受派生类中重写的影响。下面的示例对 Me 和 MyClass 进行了对比。
Class baseClass
    Public Overridable Sub testMethod()
        MsgBox("Base class string")
    End Sub
    Public Sub useMe()
        ' The following call uses the calling class's version, even if 
        ' that version is an override.
        Me.testMethod()
    End Sub
    Public Sub useMyClass()
        ' The following call uses this version and not any override.
        MyClass.testMethod()
    End Sub
End Class
Class derivedClass : Inherits baseClass
    Public Overrides Sub testMethod()
        MsgBox("Derived class string")
    End Sub
End Class
Class testClasses
    Sub startHere()
        Dim testObj As derivedClass = New derivedClass()
        ' The following call displays "Derived class string".
        testObj.useMe()
        ' The following call displays "Base class string".
        testObj.useMyClass()
    End Sub
End Class
尽管 derivedClass 重写了 testMethod,但 useMyClass 中的 MyClass 关键字使重写的影响无效,编译器会将该调用解析为 testMethod 的基类版本。
MyClass 不能在 Shared (Visual Basic) 方法内部使用,但您可以在实例方法内部使用它来访问类的共享成员。