在引用方法的地址时,我们是否应该考虑覆盖?
Class B
Inherits A
Overrides Sub Foo
Console.WriteLine("B")
End Sub
End Class
Class A
Public Sub PFoo
... AddressOf Foo ... ' WHAT WILL DO THIS METHOD??? '
End
Protected Overridable Sub Foo()
Console.WriteLine("A")
End Sub
End Class
答案 0 :(得分:3)
它将打印B.为了使你的意图更加明显,你可以放置AddressOf Me.Foo。另外,就像一个FYI,如果你把MyClass.Foo,它会打印A
Module Module1
Sub Main()
Dim b As B = New B
b.PFoo() ' prints B
Console.ReadLine()
End Sub
End Module
Public Class B
Inherits A
Protected Overrides Sub Foo()
Console.WriteLine("B")
End Sub
End Class
Public Class A
Public Sub PFoo()
Dim f As Action = New Action(AddressOf Me.Foo)
f.Invoke()
End Sub
Protected Overridable Sub Foo()
Console.WriteLine("A")
End Sub
End Class
答案 1 :(得分:2)
我相信如果在继承的B类/对象实例中调用PFoo,PFoo将引用在继承类中重写的Foo。