"MyBase" and "MyClass" Usage in VB.NET
I开发者_Go百科n what scenarios would one use the MyBase
and MyClass
keywords in VB.NET?
MyBase
is used when a virtual function needs to call its parent’s version. For example, consider:
Class Button
Public Overridable Sub Paint()
' Paint button here. '
End Sub
End Class
Class ButtonWithFancyBanner
Inherits Button
Public Overrides Sub Paint()
' First, paint the button. '
MyBase.Paint()
' Now, paint the banner. … '
End Sub
End Class
(This is the same as base
in C#.)
MyClass
is rarely used at all. It calls the own class’s method, even if it would usually call the derived class’s virtual method. In other words, it disbands the virtual method dispatching and instead makes a static call.
This is a contrived example. I’m hard-pressed to find a real-world usage now (although that certainly exists):
Class Base
Public Overridable Sub PrintName()
Console.WriteLine("I’m Base")
End Sub
Public Sub ReallyPrintMyName()
MyClass.PrintName()
End Sub
End Class
Class Derived
Inherits Base
Public Overrides Sub PrintName()
Console.WriteLine("I’m Derived")
End Sub
End Class
' … Usage: '
Dim b As Base = New Derived()
b.PrintName() ' Prints "I’m Derived" '
b.ReallyPrintMyName() ' Prints "I’m Base" '
(This doesn’t exist in C#. In IL, this issues a call
instead of the usual callvirt
opcode.)
Both are used when you need to call an virtual method and you need to specify which one. MyBase
will call the method in the base class, MyClass
will call the method in the current class.
I don't think I've used MyClass
more than once or twice, but MyBase
I've used quite a bit when I override a method but want the code in the overridden method to be executed as well.
MyBase
:
http://msdn.microsoft.com/en-us/library/dzfhkk01%28VS.71%29.aspx
MyClass
:
http://msdn.microsoft.com/en-us/library/b3b35kyk%28VS.71%29.aspx
mybase
refers to the immediate base class of the current instance.
myclass
refers to the current instance, but ignores any overridden implementation of the properties/methods of the class.
In a nutshell the overall usage of both is for Polymorphism. Here is a nice article which explains the keywords a little further.
Both terms are used under the context of a Parent-Child (or Base-Inherited) relation between two classes.
MyBase
is called from a child class. You use this when you want to refer to a member of the parent class with no ambiguity. This is useful when the child class has a member for which the name is also used in the parent class (note this means the child member either overloads
or shadows
the parent member).
MyClass
is called from the parent class. You use this when you want to refer to a member of that same parent class, and ignore whether the parent member might be overridden by a child member. This is arguably useful when you define a default behaviour by tagging a member as Overridable
, but still wish to use that default behaviour for mechanisms within the parent class.
Regarding the usage of MyClass, there is a very minor speed improvement to be had if you're using MyClass
inside of a leaf class (NotInheritable class). Specifically, if there is some method that exists in a parent class and is not overridden OR exists as an override in the leaf class, then using MyClass
instead of Me
causes a call
opcode to be used instead of callvirt
.
MyBase
, which also emits the call
opcode, isn't applicable in this case where you want the method to be invoked regardless if it's implemented/overridden in the leaf or existing only in the parent. I.e., you're maintaining a form of style consistency among a large number of similar objects.
And when I say "speed improvement", I'm talking a paltry ~5ms out of 100,000 runs on a low-end development server running in 32bit mode, on a string-parsing function. Or to state it another way, the faster the machine, the smaller this difference is going to be. But, I wanted to put it out there.
These keywords are used in scenarios involving Inheritence. MyBase, MyClass are often used to refer to some functionality or property in the parent class. If you have a class that derives from another, those keywords are used to reference code in the context of the parent class.
精彩评论