课堂上“覆盖”和“超载”之间的区别? [英] difference between 'overrides' and 'overloads' in class?

查看:73
本文介绍了课堂上“覆盖”和“超载”之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我正在学习使用类。

在类classbase1中,我定义了一个可覆盖的函数。在类

" subclass1"中,我使用''Overrides''定义了相同的函数。

在类classbase2中,我定义了相同的函数并且在这个类

" subclass2",我用''Overloads''定义了相同的函数。


结果:我得到相同的输出。


我的问题是:这两种方法有什么区别?是一个

比另一个好吗?在哪些情况下?


谢谢
$ b $bAndré


公共类classbase1


公共可覆盖函数myfunction()为字符串

返回开始

结束函数


结束类


公共类子类1

继承classbase1


公共覆盖函数myfunction()As String

返回结束

结束功能


结束课程


-------- -----------------------------------------


公共类classbase2


公共函数myfunction()字符串

返回开始

结束函数


结束班


公共类子类2

继承classbase2


公开重载函数myfunction()As String

返回"结束>

结束函数


结束类

解方案

重载一个过程是在一个类中定义了多个相同的过程,但每个定义都有不同的签名

(参数和返回)。

重写一个过程是从父类重新定义一个过程,

使它具有相同的签名,但实现方式不同。我认为,已被覆盖的

程序也可能会超载。

也许......


3月15日下午4:13,lord.zol ... @ gmail.com写道:


重载程序具有相同的程序在一个类中定义了多个

次,但是每个定义都有不同的签名

(参数和返回)。

重写一个过程就是重新定义一个来自父类的过程,

,以便它具有相同的签名,但实现方式不同。我认为,被覆盖的

程序也可能会超载。

也许......



Lord.zol是正确的。


例如:


''这是一个基类。它必须是继承的。

公共MustInherit类FooBase


''这个类被标记为可覆盖,因为我们

''想要允许派生类重新定义

''它的行为。他们没有必要,但是如果他们需要,他们可以获得b $ b''。如果他们超越它,

''他们的版本将被调用,而不是我们的。

''

''我们也标记为Overridable因为我们有另外一个同名的方法,但是

''版本需要一个参数,而这个版本

''没有。

公共超载Overridable Sub Bar()

Debug.WriteLine(" FooBase.Bar")

End Sub


Public Overloads Overridable Sub Bar(ByVal value As String)

Debug.WriteLine(" FooBase.Bar:"& value)

结束子


结束类

''这是派生类,继承FooBase。

公共类Foo

继承FooBase


''此方法标记为重载,因为它具有相同的名称,但更改了参数列表。

''如果返回类型改变,它也会这样做。

''

''基类'的版本需要一个字符串。这个

''取一个整数。

公共重载子条(ByVal值为整数)

Debug.WriteLine(" Foo。 Bar:"& CStr(value))

End Sub


''此方法标记为Overrides,因为它有

''与'/ b $ b''基类中的方法名称和签名相同,并改变了

''方法的行为。

''

''它也标记为Overloads,因为有

''其他方法在

'中同名'这个类和基类。

公共重载覆盖子栏()

Debug.WriteLine(" Foo.Bar")

结束子


结束子


希望这会有所帮助!

Mike


抱歉!错字!这是固定代码!


''这是一个基类。它必须是继承的。

公共MustInherit类FooBase


''这个类被标记为可覆盖,因为我们

''想要允许派生类重新定义

''它的行为。他们没有必要,但是如果他们需要,他们可以获得b $ b''。如果他们覆盖它,

''他们的版本将被调用,而不是我们的。

''

''我们也标记它过载因为我们有另外一个同名的方法,但是

''版本需要一个参数,而这个版本

''没有。

公共超载Overridable Sub Bar()

Debug.WriteLine(" FooBase.Bar")

End Sub


Public Overloads Overridable Sub Bar(ByVal value As String)

Debug.WriteLine(" FooBase.Bar:"& value)

结束子


结束类

''这是派生类,继承FooBase。

公共类Foo

继承FooBase


''此方法标记为重载,因为它具有相同的名称,但更改了参数列表。

''如果返回类型改变,它也会这样做。

''

''基类的版本需要一个字符串。这个

''取一个整数。

公共重载子条(ByVal值为整数)

Debug.WriteLine(" Foo。 Bar:"& CStr(value))

End Sub


''此方法标记为Overrides,因为它有

''与'/ b $ b''基类中的方法名称和签名相同,并改变了

''方法的行为。

''

''它也标记为Overloads,因为有

''其他方法在

'中同名'这个类和基类。

公共重载覆盖子栏()

Debug.WriteLine(" Foo.Bar")

结束子


结束子


Hi,

i''m learning working with classes.
In class "classbase1", i defined an overridable function. In the class
"subclass1", i defined the same function with ''Overrides''.
In class "classbase2", i defined the same function and in the class
"subclass2", i defined the same function with ''Overloads''.

Result: i get the same output.

My question is: what''s the difference between the two approaches? Is one
better than the other? In which cases?

Thanks
André

Public Class classbase1

Public Overridable Function myfunction() As String
Return "start"
End Function

End Class

Public Class subclass1
Inherits classbase1

Public Overrides Function myfunction() As String
Return "end"
End Function

End Class

-------------------------------------------------

Public Class classbase2

Public Function myfunction() As String
Return "start"
End Function

End Class

Public Class subclass2
Inherits classbase2

Public Overloads Function myfunction() As String
Return "end"
End Function

End Class

解决方案

Overloading a procedure is having the same procedure defined multiple
times in a class, but each definition has a different signature
(arguments and return).
Overriding a procedure is redefining a procedure from a parent class,
so that it has the same signature, but different implementation. A
procedure that''s been overridden can also be overloaded, I think.
maybe...


On Mar 15, 4:13 pm, lord.zol...@gmail.com wrote:

Overloading a procedure is having the same procedure defined multiple
times in a class, but each definition has a different signature
(arguments and return).
Overriding a procedure is redefining a procedure from a parent class,
so that it has the same signature, but different implementation. A
procedure that''s been overridden can also be overloaded, I think.
maybe...

Lord.zol is correct.

For example:

'' This is a base class. It must be inherited.
Public MustInherit Class FooBase

'' This class is marked Overridable because we
'' want to allow derived classes to redefine
'' it''s behavior. They don''t HAVE to, but they
'' can if they need to. If they DO override it,
'' their version will be called, not ours.
''
'' We also mark it Overridable because we have
'' another method with the same name, but that
'' version takes a parameter, while this version
'' does not.
Public Overloads Overridable Sub Bar()
Debug.WriteLine("FooBase.Bar")
End Sub

Public Overloads Overridable Sub Bar(ByVal value As String)
Debug.WriteLine("FooBase.Bar: " & value)
End Sub

End Class
'' This is a derived class, inheriting FooBase.
Public Class Foo
Inherits FooBase

'' This method is marked overloads because it has
'' the same name, but changes the argument list.
'' It would do the same if the return type changed.
''
'' The base class''s version takes a string. This
'' one takes an integer.
Public Overloads Sub Bar(ByVal value As Integer)
Debug.WriteLine("Foo.Bar: " & CStr(value))
End Sub

'' This method is marked Overrides because it has
'' the same name AND signature as a method in the
'' base class, and changes the behavior of that
'' method.
''
'' It is also marked Overloads because there are
'' other methods that have the same name both in
'' this class and the base class.
Public Overloads Overrides Sub Bar()
Debug.WriteLine("Foo.Bar")
End Sub

End Sub

Hope this helps!
Mike


Sorry! Typo! Here''s the fixed code!

'' This is a base class. It must be inherited.
Public MustInherit Class FooBase

'' This class is marked Overridable because we
'' want to allow derived classes to redefine
'' it''s behavior. They don''t HAVE to, but they
'' can if they need to. If they DO override it,
'' their version will be called, not ours.
''
'' We also mark it Overloads because we have
'' another method with the same name, but that
'' version takes a parameter, while this version
'' does not.
Public Overloads Overridable Sub Bar()
Debug.WriteLine("FooBase.Bar")
End Sub

Public Overloads Overridable Sub Bar(ByVal value As String)
Debug.WriteLine("FooBase.Bar: " & value)
End Sub

End Class
'' This is a derived class, inheriting FooBase.
Public Class Foo
Inherits FooBase

'' This method is marked overloads because it has
'' the same name, but changes the argument list.
'' It would do the same if the return type changed.
''
'' The base class''s version takes a string. This
'' one takes an integer.
Public Overloads Sub Bar(ByVal value As Integer)
Debug.WriteLine("Foo.Bar: " & CStr(value))
End Sub

'' This method is marked Overrides because it has
'' the same name AND signature as a method in the
'' base class, and changes the behavior of that
'' method.
''
'' It is also marked Overloads because there are
'' other methods that have the same name both in
'' this class and the base class.
Public Overloads Overrides Sub Bar()
Debug.WriteLine("Foo.Bar")
End Sub

End Sub


这篇关于课堂上“覆盖”和“超载”之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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