![](/img/trans.png)
[英]What does “<employee>” mean in the following code: List<Employee><employee> employees = new List<Employee><employee>();
[英]what the new mean in the following
在界面中,我看到了以下内容
public interface ITest : ITestBase
{
new string Item { get; set; }
}
我想在这里知道“新”的含义。
属性或方法前面的新关键字用于隐藏非虚拟的父类中具有相同名称的成员,并且被许多(包括我)认为是不良做法,因为在许多情况下它可能不会给出你期望的结果。
一个例子:
class A{
public int Test(){ return 1; }
}
class B : A{
public new int Test(){ return 2; }
}
B b = new B();
Console.WriteLine( b.Test() );
A b2 = new B();
Console.WriteLine( b2.Test() );
这将分别打印2和1,并且令人困惑,因为这两个对象实际上都是B类。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.