这里很简单,你有我的vb.net代码:
Public Class Class1
Public Overridable ReadOnly Property Name() As String
Get
Return Nothing
End Get
End Property
End Class
Public Class Class2
Inherits Class1
Public Overloads ReadOnly Property Name() As String
Get
Return "Class2"
End Get
End Property
End Class
Module Module1
Sub Main()
Dim c2 As New Class2()
Console.WriteLine(c2.Name)
Dim c1 As Class1 = CType(c2, Class1)
Console.WriteLine(c1.Name)
End Sub
End Module
这里有c#代码:
class Class1
{
public virtual string Name
{
get
{
return null;
}
}
}
class Class2 : Class1
{
public override string Name
{
get
{
return "Class2";
}
}
}
class Program
{
static void Main(string[] args)
{
Class2 c2 = new Class2();
Console.WriteLine(c2.Name);
Class1 c1 = (Class1)c2;
Console.WriteLine(c1.Name);
}
}
我确实让他们做同样的事情,但猜猜他们不要做什么! C#输出
的Class2
的Class2
VB.NET输出
的Class2
{没什么}
(只打印一条空行,不打印任何东西) 为什么vb.net在覆盖时会用于基类实现?
答案 0 :(得分:12)
是否应该覆盖而不是超载?
Public Overrides ReadOnly Property Name() As String