Vb.net 在vb中调用*基类内的*方法时,与c#virtual等价
我了解如何使用VB的Vb.net 在vb中调用*基类内的*方法时,与c#virtual等价,vb.net,oop,overriding,Vb.net,Oop,Overriding,我了解如何使用VB的可重写和重写,在调用类的方法时获得与c#的虚拟类似的功能。但是,请考虑下面的c++控制台代码,它调用从 > >类本身:的重写方法。 class Program { static void Main(string[] args) { new test(); new test2(); Console.ReadKey(); } } public class test { public test() { hello(); } p
可重写
和重写
,在调用类的方法时获得与c#的虚拟
类似的功能。但是,请考虑下面的c++控制台代码,它调用从<强> > <强> >类本身:的重写方法。
class Program {
static void Main(string[] args) {
new test();
new test2();
Console.ReadKey();
}
}
public class test {
public test() {
hello();
}
public virtual void hello() {
Console.WriteLine("hello from base method");
}
}
class test2 : test {
public override void hello() {
Console.WriteLine("hello from overridden method");
}
}
可以预见,我在c#中得到的结果是:
来自基本方法的您好来自重写方法的hello 问题是,我不知道如何在VB.NET中复制此功能。请记住,
hello()
是从运行重写方法的基类中调用的。这是我在VB中似乎无法实现的
无论我在VB中尝试什么,都会调用基类的hello(),而不是重写的hello()。类测试:
Public Class Test
Public Sub New()
Hello()
End Sub
Public Overridable Sub Hello()
Console.WriteLine("hello from base method")
End Sub
End Class
类Test2:
Public Class Test2
Inherits Test
Public Overrides Sub Hello()
Console.WriteLine("hello from overridden method")
End Sub
End Class
副标题:
Sub Main()
Dim x As New Test
Dim y As New Test2
End Sub
课堂测试:
Public Class Test
Public Sub New()
Hello()
End Sub
Public Overridable Sub Hello()
Console.WriteLine("hello from base method")
End Sub
End Class
类Test2:
Public Class Test2
Inherits Test
Public Overrides Sub Hello()
Console.WriteLine("hello from overridden method")
End Sub
End Class
副标题:
Sub Main()
Dim x As New Test
Dim y As New Test2
End Sub
你能展示你试过的VB代码吗?你能展示你试过的VB代码吗?