VB.NET 中的“MyBase”和“MyClass”用法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2849329/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"MyBase" and "MyClass" Usage in VB.NET
提问by Nehil Verma
In what scenarios would one use the MyBase
and MyClass
keywords in VB.NET?
在什么情况下会在 VB.NET 中使用MyBase
andMyClass
关键字?
回答by Konrad Rudolph
MyBase
is used when a virtual function needs to call its parent's version. For example, consider:
MyBase
当虚函数需要调用其父版本时使用。例如,考虑:
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#.)
(这与base
C# 中的相同。)
MyClass
is rarely used at all. It calls the ownclass's method, even if it would usually call the derived class'svirtual method. In other words, it disbands the virtual method dispatching and instead makes a static call.
MyClass
很少使用。它调用自己类的方法,即使它通常会调用派生类的虚方法。换句话说,它解散了虚拟方法调度,而是进行静态调用。
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.)
(这在 C# 中不存在。在 IL 中,这会发出 acall
而不是通常的callvirt
操作码。)
回答by Hans Olsson
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.
当您需要调用虚拟方法并且需要指定哪一个时,两者都会使用。MyBase
将调用基类中MyClass
的方法,将调用当前类中的方法。
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.
我不认为我使用MyClass
过一次或两次以上,但是MyBase
当我覆盖一个方法但希望被覆盖的方法中的代码也被执行时,我已经使用了很多。
MyBase
:
MyBase
:
http://msdn.microsoft.com/en-us/library/dzfhkk01%28VS.71%29.aspx
http://msdn.microsoft.com/en-us/library/dzfhkk01%28VS.71%29.aspx
MyClass
:
MyClass
:
http://msdn.microsoft.com/en-us/library/b3b35kyk%28VS.71%29.aspx
http://msdn.microsoft.com/en-us/library/b3b35kyk%28VS.71%29.aspx
回答by James
mybase
refers to the immediate base class of the current instance.
mybase
指的是当前实例的直接基类。
myclass
refers to the current instance, but ignores any overridden implementation of the properties/methods of the class.
myclass
指的是当前实例,但忽略类的属性/方法的任何覆盖实现。
In a nutshell the overall usage of both is for Polymorphism. Here is a nice articlewhich explains the keywords a little further.
回答by Kumba
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
.
关于 MyClass 的使用,如果您在MyClass
叶类(NotInheritable 类)内部使用,速度会有很小的提升。具体来说,如果有一些方法存在于父类中并且未被覆盖或作为覆盖存在于叶类中,则使用MyClass
而不是Me
导致使用call
操作码而不是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.
MyBase
,它也发出call
操作码,不适用于您希望调用该方法的情况,无论它是在叶中实现/覆盖还是仅存在于父中。即,您要在大量相似对象之间保持某种形式的样式一致性。
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.
当我说“速度改进”时,我指的是在以 32 位模式运行的低端开发服务器上,在字符串解析功能上运行 100,000 次中的 5 毫秒。或者换句话说,机器越快,这种差异就越小。但是,我想把它放在那里。
回答by Ama
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).
MyBase
从子类调用。当您想无歧义地引用父类的成员时,可以使用它。当子类具有在父类中也使用其名称的成员时,这很有用(请注意,这意味着子成员overloads
或shadows
父成员)。
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 arguablyuseful 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.
MyClass
从父类调用。当您想引用同一个父类的成员时,您可以使用它,并忽略父成员是否可能被子成员覆盖。当您通过将成员标记为 来定义默认行为,但仍希望将该默认行为用于父类中的机制时,这可以说是有用的Overridable
。
回答by Achilles
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.
这些关键字用于涉及继承的场景。MyBase、MyClass 常用于指代父类中的某些功能或属性。如果您有一个派生自另一个类的类,则这些关键字用于在父类的上下文中引用代码。