问题描述
你真的需要这个关键字来重载方法吗?使用重载关键字与仅使用不同的方法签名有什么区别?
Do you really need this keyword to overload methods? What is the difference between using the overloads keyword vs. just having different method signatures?
推荐答案
在同一个类中,Overloads
关键字是可选的,但如果声明了一个方法 Overloads
或Overrides
,您必须将它用于该方法的所有重载.
Within the same class, the Overloads
keyword is optional, but if one method is declared Overloads
or Overrides
, you must use it for all overloads of that method.
' this is okay
Sub F1(s as String)
Sub F1(n as Integer)
' This is also okay
Overloads Sub F2(s as String)
Overloads Sub F2(n as Integer)
' Error
Overloads Sub F3(s as String)
Sub F3(n as Integer)
然而,当您在派生类中重载基类方法时,情况会变得更加复杂.
It gets more complicated when you are overloading base class methods in a derived class however.
如果一个基类有多个重载方法,并且你想在派生类中添加一个重载方法,那么你必须用Overloads
关键字标记派生类中的方法,否则所有的重载基类中的方法在派生类中不可用.
If a base class has several overloaded methods and you want to add an overloaded method in a derived class, then you must mark the method in the derived class with the Overloads
keyword, otherwise all the overloaded methods in the base class are unavailable in the derived class.
有关详细信息,请参阅 MSDN.
See MSDN for more details.
这篇关于“超载"VB.NET 中的关键字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!