如何“超载"在儿童班上工作? [英] How does "Overloads" work in a child class?
问题描述
我有一个基类和一个子类,它们都具有相同的属性,我不理解为什么,VB希望我对子类中的属性使用重载".不同之处在于,该属性的子类版本为Shared
,而父类基本上位于该结构中.这些属性如下所示:
I have a base class and a child class and they both have the same property and I don't understand why VB wants me to use "Overloads" for the property in the child class. The difference is the child class version of the property is Shared
while the parent class is basically there for structure. The properties look like this:
Public MustInherit Class Parent
Public ReadOnly Property Species As String
Get
Return "Should get species from a child."
End Get
End Property
End Class
Public Class Child
Inherits Parent
Public Shared ReadOnly Property Species As String
Get
Return "Species1"
End Get
End Property
End Class
Species
在子类的行Public Shared ReadOnly Property Species As String
中标记为警告消息
Species
is flagged in the line Public Shared ReadOnly Property Species As String
in the child class with the warning message
属性'Species'遮盖了在基声明中声明的可重载成员 父类.如果要重载基本方法,则此方法 必须声明为过载".
property 'Species' shadows an overloadable member declared in the base class 'Parent'. If you want to overload the base method, this method must be declared 'Overloads'.
我想知道的是为什么?当将不同的参数传递到具有相同名称的函数中时,通常会使用重载,但有据可查,但是我发现没有什么能解释为什么在这种情况下突然建议重载的原因.
What I want to know is why does it want this to be overloaded? Overloading is typically used when different parameters are being passed into functions with the same name which is well documented, but I've found nothing explaining why overloads is suddenly suggested in a situation like this.
注意:无论代码是否具有过载",代码都会正确报告"Species1" ...
Note: that the code properly reports "Species1" regardless of if have the "Overloads" or not adding to my confusion of what it actually does...
推荐答案
如果要重载基本方法,则必须将该方法声明为重载".
If you want to overload the base method, this method must be declared 'Overloads'.
该错误消息过于笼统.请注意,即使警告是关于属性的,它也将谈论方法.您不能重载属性.
The error message is too generic. Note how it talks about a method even though the warning is about a property. You cannot overload a property.
如果我是法国国王,那我会写如下错误消息:
If I were the King of France, I would have written the error message like:
属性'Species'隐藏了从'Parent'基类继承的'Species'属性.如果要隐藏,请使用Shadows关键字禁止显示此警告.如果不希望隐藏,请更改属性的名称.
Property 'Species' hides the 'Species' property inherited from the 'Parent' base class. Use the Shadows keyword to suppress this warning if hiding was intended. Change the name of the property if hiding was not intended.
几乎永远不应忽略此警告,因为它几乎总是标识代码气味.为Child.Species使用 Shared 关键字非常奇怪,几乎可以肯定不是您想要的.任何通过类型为Parent的引用使用Child对象的代码都将始终获得错误的种类名称,因为它将使用base属性.此处更明智的做法是声明Parent.Species属性 Overridable ,并在Child.Species属性声明中使用 Overrides 关键字,而不使用Shared.
This warning should almost never be ignored because is almost always identifies a code smell. Using the Shared keyword for Child.Species is very strange and almost certainly not what you intended. Any code that uses your Child object through a reference of type Parent will always get the wrong species name since it will use the base property. The more sane thing to do here is to declare the Parent.Species property Overridable and use the Overrides keyword in Child.Species property declaration, without Shared.
这篇关于如何“超载"在儿童班上工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!