重载覆盖 [英] Overloads Overrides

查看:59
本文介绍了重载覆盖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我来这里是为了解决Overloads Overrides中的一个小混乱。


" Overloading"说方法的名称应该相同而不是。

参数和/或它们的数据类型应该在相同的

类或继承类中更改。对吗?


" Overriding"说继承类中方法的签名应该是相同的(名称,编号,参数及其数据类型的b $ b),而实现可能会改变。对吧?


我们如何同时实现这两个目标,比如


[Visual Basic.NET]

重载覆盖公共子DoSomething()


在这里,如果我们更改签名,我们就无法实现覆盖,如果我们不知道,那么我们就不会这样做t更改签名然后我们不能实现重载但是它发生了b $ b。如何?

VB.NET是否改变了重载和覆盖的概念或

我错了:(:?


参考:
http://msdn.microsoft.com/library/de...elinetopic.asp


有人会说清楚这是怎么做的。

谢谢

解决方案

Atif,

重载覆盖Public Sub DoSomething()
说你没有参数覆盖MyBase.DoSomething(),而

a MyBase.DoSomething(...)或MyClass.DoSomething(...)参数

存在。


希望这有帮助

Jay


" Atif" at **** ***@hotmail.com>在留言中写道

新闻:3c ************************** @发布.google.c om ...嗨
我我在这里解决我在Overloads Overrides中遇到的小混乱。

重载说方法的名称应该相同而不是。
参数和/或其数据类型应在同一类或继承类中更改。对吗?

覆盖表示方法的签名在继承的类中应该是相同的(名称,编号和参数及其数据类型),而实现可能会改变。对吧?

我们如何能够同时实现这两个目标,如

[Visual Basic.NET]
重载覆盖Public Sub DoSomething()

在这里,如果我们改变签名,我们就不能实现覆盖,如果我们不改变签名,那么我们就不能实现重载但是它正在发生。如何?
VB.NET是否改变了重载和覆盖的概念或者我错了:(:?

参考:
http://msdn.microsoft。 com / library / de ... elinetopic.asp

有人会说清楚这是怎么做的。
谢谢



Jay B. Harlow [MVP - Outlook]写道:

Atif,

重载覆盖Public Sub DoSomething ()



说你没有参数覆盖MyBase.DoSomething(),而是一个MyBase.DoSomething(...)或MyClass.DoSomething(... )参数
存在。




编译器不同意你:


Public Class Foo

Public Overrides Function Equals(ByVal ot她作为对象)作为布尔值

结束函数

结束类


编译器为此代码发出以下警告:


警告BC40003:函数''等于''隐藏在基类''Object''中声明的可重写成员

。如果你想重载基本方法,这个方法

必须声明''Overloads''。


唯一涉及的Equals()方法是System.Object和Foo上的那些,

并且不存在具有不同参数的Equals()。


然而,除非我写信,否则我会收到警告:


公共重载函数等于(ByVal其他作为对象)作为布尔值


然而,我没有超载Equals,我压倒一切它:派生类中的函数签名

与基类中的函数签名匹配。


另一种消除警告的方法是使用:


公共重载覆盖函数等于(ByVal其他作为对象)作为布尔值


但是,再一次,我不会超载任何东西,我''重写基类方法。


似乎编译器与文档冲突,文档说:


重载

可选。表示此Function过程重载在基类中使用相同名称定义的一个或多个

过程。此声明中的参数列表

必须与每个

重载过程的参数列表不同。列表必须在参数数量上有所不同,

他们的数据类型,或两者兼而有之。这允许编译器区分使用哪个

版本。


覆盖

可选。表示此Function过程在基类中覆盖相同的

命名过程。参数的数量和数据类型

和返回值的数据类型必须与基本

类过程完全匹配。


因此,在Equals的情况下,重载不适用,但编译器会发出警告

,除非存在Overloads关键字。似乎编译器需要修复?


干杯,


Michi。


Jay

重载覆盖Public Sub DoSomething()
说你重写了MyBase.DoSomething()没有参数,而存在参数
的MyBase.DoSomething(...)或MyClass.DoSomething(...)。



让我们假设MyBase中有这三种方法

1- DoSomething()

2- DoSomething(a整数)

3- DoSomething(a为整数,b为整数)


如果你写

覆盖Public Sub DoSomething(a为整数)

你准确指定你要覆盖哪种方法,即第二,

无需添加重载,因为没有超载

Michi

编译器不同意您的意见:
。 。 。似乎编译器需要修复?




我同意,但是,我们需要更多评论


Hi
I am here to solve a small confusion i have in "Overloads Overrides".

"Overloading" says that the method''s name should be same while no. of
parameters and/or their datatypes should be changed either in the same
class or inherited class. right?

"Overriding" says that the method''s signature should be same(name,no.
of parameters and their datatypes) in the inherited class while
implementation may change. right?

How we can implement both of these all at the same time like

[Visual Basic.NET]
Overloads Overrides Public Sub DoSomething()

Here, if we change the signature we can''t implement Overriding and if
we don''t change signature then we can''t implement Overloading BUT It
is happening. How?
Has VB.NET changed the concept of overloading and overriding or
I AM WRONG :(: ?

Ref:
http://msdn.microsoft.com/library/de...elinetopic.asp

Would someone make it clear how this is being done.
Thanks

解决方案

Atif,

Overloads Overrides Public Sub DoSomething() Says that you are overriding MyBase.DoSomething() with no parameters, while
a MyBase.DoSomething(...) or MyClass.DoSomething(...) with parameters
exists.

Hope this helps
Jay

"Atif" <at*******@hotmail.com> wrote in message
news:3c**************************@posting.google.c om... Hi
I am here to solve a small confusion i have in "Overloads Overrides".

"Overloading" says that the method''s name should be same while no. of
parameters and/or their datatypes should be changed either in the same
class or inherited class. right?

"Overriding" says that the method''s signature should be same(name,no.
of parameters and their datatypes) in the inherited class while
implementation may change. right?

How we can implement both of these all at the same time like

[Visual Basic.NET]
Overloads Overrides Public Sub DoSomething()

Here, if we change the signature we can''t implement Overriding and if
we don''t change signature then we can''t implement Overloading BUT It
is happening. How?
Has VB.NET changed the concept of overloading and overriding or
I AM WRONG :(: ?

Ref:
http://msdn.microsoft.com/library/de...elinetopic.asp

Would someone make it clear how this is being done.
Thanks



Jay B. Harlow [MVP - Outlook] wrote:

Atif,

Overloads Overrides Public Sub DoSomething()



Says that you are overriding MyBase.DoSomething() with no parameters, while
a MyBase.DoSomething(...) or MyClass.DoSomething(...) with parameters
exists.



The compiler does not agree with you:

Public Class Foo
Public Overrides Function Equals(ByVal other As Object) As Boolean
End Function
End Class

The compiler issues the following warning for this code:

warning BC40003: function ''Equals'' shadows an overloadable member declared in
the base class ''Object''. If you want to overload the base method, this method
must be declared ''Overloads''.

The only Equals() methods that are involved are the ones on System.Object and Foo,
and no Equals() with different parameters exists.

Yet, I get the warning unless I write:

Public Overloads Function Equals(ByVal other As Object) As Boolean

However, I''m not overloading Equals, I''m overriding it: the function signature
in the derived class matches the function signature in the base class.

Another way to get rid of the warning is to use:

Public Overloads Overrides Function Equals(ByVal other As Object) As Boolean

But, again, I''m not overloading anything, I''m overriding the base class method.

It seems that the compiler is in conflict with the documentation, which says:

Overloads
Optional. Indicates that this Function procedure overloads one or more
procedures defined with the same name in a base class. The argument list
in this declaration must be different from the argument list of every
overloaded procedure. The lists must differ in the number of arguments,
their data types, or both. This allows the compiler to distinguish which
version to use.

Overrides
Optional. Indicates that this Function procedure overrides an identically
named procedure in a base class. The number and data types of the arguments,
and the data type of the return value, must exactly match those of the base
class procedure.

So, overloading does not apply in case of Equals, yet the compiler issues the warning
unless the Overloads keyword is present. Seems that the compiler needs fixing?

Cheers,

Michi.


Jay

Overloads Overrides Public Sub DoSomething()
Says that you are overriding MyBase.DoSomething() with no parameters, while
a MyBase.DoSomething(...) or MyClass.DoSomething(...) with parameters
exists.



Let us suppose these three methods are there in MyBase
1- DoSomething()
2- DoSomething(a as integer)
3- DoSomething(a as integer, b as integer)

If you write
Overrides Public Sub DoSomething(a as integer)
you are specifying exactly which method you are overriding i.e. 2nd,
no need to add Overloads as there is no overloading
Michi
The compiler does not agree with you:
. . . Seems that the compiler needs fixing?



I agree, but, we require more comments


这篇关于重载覆盖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆