在执行重构时,我最终创建了一个类似下面示例的方法。为简单起见,数据类型已更改。
我之前有这样的作业声明:
MyObject myVar = new MyObject();
这是偶然的重构:
private static new MyObject CreateSomething()
{
return new MyObject{"Something New"};
}
这是我的剪切/粘贴错误的结果,但new
中的private static new
关键字有效并且已编译。
问题:new
关键字在方法签名中表示什么?我假设它是在C#3.0中引入的东西?
这与override
有什么不同?
答案 0 :(得分:93)
来自MSDN的新关键字参考:
以下是我在网上从Microsoft MVP中发现的一个很有意义的例子: Link to Original
public class A
{
public virtual void One();
public void Two();
}
public class B : A
{
public override void One();
public new void Two();
}
B b = new B();
A a = b as A;
a.One(); // Calls implementation in B
a.Two(); // Calls implementation in A
b.One(); // Calls implementation in B
b.Two(); // Calls implementation in B
覆盖只能在非常特殊的情况下使用。来自MSDN:
您无法覆盖非虚拟或 静态方法。被覆盖的基地 方法必须是虚拟的,抽象的或 覆盖。
因此需要'new'关键字来“覆盖”非虚拟和静态方法。
答案 1 :(得分:57)
不,它实际上不是“新的”(原谅双关语)。它基本上用于“隐藏”一种方法。 IE:
public class Base
{
public virtual void Method(){}
}
public class Derived : Base
{
public new void Method(){}
}
如果你这样做:
Base b = new Derived();
b.Method();
Base中的方法是将被调用的方法,而不是派生的方法。
更多信息:http://www.akadia.com/services/dotnet_polymorphism.html
重新编辑:在我给出的示例中,如果您要“覆盖”而不是使用“新”,那么当您调用b.Method()时;由于多态性,将调用Derived类的Method。
答案 2 :(得分:22)
正如其他人所解释的那样,它用于隐藏现有方法。它可用于覆盖父类中非虚拟的方法。
请记住,创建“新”成员不是多态的。如果将对象强制转换为基类型,则不会使用派生类型的成员。
如果您有基类:
public class BaseClass
{
public void DoSomething() { }
}
然后是派生类:
public class DerivedType : BaseClass
{
public new void DoSomething() {}
}
如果声明类型DerivedType
然后强制转换它,方法DoSomething()
不是多态的,它将调用基类的方法,而不是派生的方法。
BaseClass t = new DerivedType();
t.DoSomething();// Calls the "DoSomething()" method of the base class.
答案 3 :(得分:7)
来自文档:
如果派生类中的方法前面带有new关键字,则该方法被定义为独立于基类中的方法。
这在实践中意味着什么:
如果从其他类继承并且您有一个共享相同签名的方法,则可以将其定义为“new”,以使其独立于父类。这意味着如果你有对'parent'类的引用,那么将执行该实现,如果你有对子类的引用,那么将执行该实现。
就个人而言,我试图避免使用'new'关键字,因为它通常意味着我的类层次结构错误,但有时它可能有用。一个地方是版本控制和向后兼容性。
the MSDN for this中有很多信息。
答案 4 :(得分:3)
这意味着该方法使用基类继承的相同名称替换方法。在您的情况下,您可能在基类中没有该名称的方法,这意味着新关键字完全是多余的。
答案 5 :(得分:3)
长话短说 - 它不是必需的,它会改变NO行为,并且为了便于阅读,它很自然。
这就是为什么在VS中你会看到一点点波浪形,但你的代码将编译并运行完全正常并且符合预期。
人们不禁要问,创建new
关键字是否真的值得开发人员承认“是的,我知道我隐藏了一个基本方法,是的,我知道我什么都没做与virtual
或overriden
(多态)相关 - 我真的想创建它自己的方法“。
这对我来说有点奇怪,但也许只是因为我来自Java
背景,C#
继承与Java
之间存在根本区别:在Java
中,默认情况下,方法是虚拟的,除非final
指定。在C#
中,除非由virtual
指定,否则方法默认为final /具体。
答案 6 :(得分:1)
来自MSDN:
使用new修饰符显式隐藏从a继承的成员 基类。要隐藏继承的成员,请在派生中声明它 使用相同名称的类,并使用new修饰符修改它。
答案 7 :(得分:0)
小心这个问题 您有一个在基类中实现的接口中定义的方法。然后创建一个隐藏接口方法的派生类,但不要将派生类声明为实现接口。如果然后通过对接口的引用调用该方法,则将调用基类的方法。 但是,如果派生类确实实现了接口,那么无论使用哪种类型的引用,都会调用其方法。
interface IMethodToHide
{
string MethodToHide();
}
class BaseWithMethodToHide : IMethodToHide
{
public string MethodToHide()
{
return "BaseWithMethodToHide";
}
}
class DerivedNotImplementingInterface : BaseWithMethodToHide
{
new public string MethodToHide()
{
return "DerivedNotImplementingInterface";
}
}
class DerivedImplementingInterface : BaseWithMethodToHide, IMethodToHide
{
new public string MethodToHide()
{
return "DerivedImplementingInterface";
}
}
class Program
{
static void Main()
{
var oNoI = new DerivedNotImplementingInterface();
IMethodToHide ioNoI = new DerivedNotImplementingInterface();
Console.WriteLine("reference to the object type DerivedNotImplementingInterface calls the method in the class "
+ oNoI.MethodToHide());
// calls DerivedNotImplementingInterface.MethodToHide()
Console.WriteLine("reference to a DerivedNotImplementingInterface object via the interfce IMethodToHide calls the method in the class "
+ ioNoI.MethodToHide());
// calls BaseWithMethodToHide.MethodToHide()
Console.ReadLine();
var oI = new DerivedImplementingInterface();
IMethodToHide ioI = new DerivedImplementingInterface();
Console.WriteLine("reference to the object type DerivedImplementingInterface calls the method in the class "
+ oI.MethodToHide());
// calls DerivedImplementingInterface.MethodToHide()
Console.WriteLine("reference to a DerivedImplementingInterface object via the interfce IMethodToHide calls the method in the class "
+ ioI.MethodToHide());
// calls DerivedImplementingInterface.MethodToHide()
Console.ReadLine();
}
}
答案 8 :(得分:0)
FWIW,我已经编写 C# 代码很长时间了,我需要 new
关键字的唯一情况是以下 OO 层次结构方案:
public abstract class Animal
{
protected Animal(Animal mother, Animal father)
{
Mother = mother;
Father = father;
}
public Animal Mother { get; }
public Animal Father { get; }
}
public class Dog : Animal
{
public Dog(Dog mother, Dog father)
: base(mother, father)
{
}
public new Dog Mother => (Dog)base.Mother;
public new Dog Father => (Dog)base.Father;
}
几点说明:
virtual
,它们不需要标记。new
关键字是因为我们不能仅通过更改其返回类型来“覆盖”属性(或方法)。 new
是拥有良好 OO 层次结构的唯一方法。