在基本类型“virtual”中声明一个方法,然后在子类型中使用“override”关键字重写它,与在子类型中声明匹配的方法时简单地使用“new”关键字相比,两者之间有什么区别?
当前回答
下面是一些代码来理解虚拟方法和非虚拟方法行为的区别:
class A
{
public void foo()
{
Console.WriteLine("A::foo()");
}
public virtual void bar()
{
Console.WriteLine("A::bar()");
}
}
class B : A
{
public new void foo()
{
Console.WriteLine("B::foo()");
}
public override void bar()
{
Console.WriteLine("B::bar()");
}
}
class Program
{
static int Main(string[] args)
{
B b = new B();
A a = b;
a.foo(); // Prints A::foo
b.foo(); // Prints B::foo
a.bar(); // Prints B::bar
b.bar(); // Prints B::bar
return 0;
}
}
其他回答
“new”关键字不会重写,它表示一个与基类方法无关的新方法。
public class Foo
{
public bool DoSomething() { return false; }
}
public class Bar : Foo
{
public new bool DoSomething() { return true; }
}
public class Test
{
public static void Main ()
{
Foo test = new Bar ();
Console.WriteLine (test.DoSomething ());
}
}
它输出false,如果你使用override,它会输出true。
(基本代码取自Joseph Daigle)
所以,如果你在做真正的多态性,你应该总是OVERRIDE。唯一需要使用“new”的地方是当方法与基类版本没有任何关联时。
new关键字实际上创建了一个全新的成员,该成员只存在于该特定类型上。
例如
public class Foo
{
public bool DoSomething() { return false; }
}
public class Bar : Foo
{
public new bool DoSomething() { return true; }
}
该方法存在于这两种类型上。当您使用反射并获取Bar类型的成员时,您实际上会发现两个称为DoSomething()的方法看起来完全相同。通过使用new,你可以有效地将实现隐藏在基类中,这样当类从Bar派生出来时(在我的例子中),对base. dosomething()的方法调用就会到Bar而不是Foo。
Virtual / override告诉编译器这两个方法是相关的,在某些情况下,当你认为你在调用第一个(虚拟)方法时,实际上应该调用第二个(被重写)方法。这是多态性的基础。
(new SubClass() as BaseClass).VirtualFoo()
将调用子类的重载VirtualFoo()方法。
New告诉编译器,您正在向派生类添加与基类中的方法同名的方法,但它们之间没有关系。
(new SubClass() as BaseClass).NewBar()
将调用BaseClass的NewBar()方法,而:
(new SubClass()).NewBar()
将调用子类的NewBar()方法。
我总是觉得这样的事情用图片更容易理解:
再一次,用joseph daigle的密码,
public class Foo
{
public /*virtual*/ bool DoSomething() { return false; }
}
public class Bar : Foo
{
public /*override or new*/ bool DoSomething() { return true; }
}
如果你像这样调用代码:
Foo a = new Bar();
a.DoSomething();
注意:重要的是,我们的对象实际上是一个Bar,但我们将它存储在类型为Foo的变量中(这类似于强制转换它)。
那么结果将如下所示,这取决于您在声明类时使用的是virtual/override还是new。
我的解释来自于使用属性来帮助理解差异。
重写很简单,对吧?底层类型覆盖父类型。
新可能是误导(对我来说是)。使用属性更容易理解:
public class Foo
{
public bool GetSomething => false;
}
public class Bar : Foo
{
public new bool GetSomething => true;
}
public static void Main(string[] args)
{
Foo foo = new Bar();
Console.WriteLine(foo.GetSomething);
Bar bar = new Bar();
Console.WriteLine(bar.GetSomething);
}
使用调试器,你可以注意到Foo Foo有两个GetSomething属性,因为它实际上有两个版本的属性,Foo's和Bar's,为了知道使用哪个,c#“选择”当前类型的属性。
如果你想使用Bar的版本,你应该使用override或使用Foo Foo代替。
Bar Bar只有1,因为它想要GetSomething的全新行为。
推荐文章
- dynamic (c# 4)和var之间的区别是什么?
- Visual Studio: ContextSwitchDeadlock
- 返回文件在ASP。Net Core Web API
- 自定义HttpClient请求头
- 如果我使用OWIN Startup.cs类并将所有配置移动到那里,我是否需要一个Global.asax.cs文件?
- VS2013外部构建错误"error MSB4019: The imported project <path> was not found"
- 从另一个列表id中排序一个列表
- 等待一个无效的异步方法
- 覆盖或覆盖
- 无法加载文件或程序集…参数不正确
- c#中枚举中的方法
- Javascript函数前导bang !语法
- 如何从字符串中删除新的行字符?
- 如何设置一个默认值与Html.TextBoxFor?
- 检查属性是否有属性