virtual、override、new、sealed override的区别 [英] The difference between virtual, override, new and sealed override

查看:42
本文介绍了virtual、override、new、sealed override的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 OOP 的一些概念感到很困惑:virtualoverridenewsealed override.任何人都可以解释这些差异吗?

I'm pretty confused between some concepts of OOP: virtual, override, new and sealed override. Can anyone explain the differences?

我很清楚,如果要使用派生类方法,可以使用override关键字,这样派生类将覆盖基类方法.但我不确定 newsealed override.

I am pretty clear that if the derived class method is to be used, one can use the override keyword so that the base class method will be overriden by derived class. But I'm not sure about new, and sealed override.

推荐答案

virtual 关键字用于修改方法、属性、索引器或事件声明,并允许它在派生的班级.例如,此方法可以被任何继承它的类覆盖:使用 new 修饰符显式隐藏从基类继承的成员.要隐藏继承的成员,请在派生类中使用相同的名称对其进行声明,并使用 new 修饰符对其进行修改.

The virtual keyword is used to modify a method, property, indexer or event declaration, and allow it to be overridden in a derived class. For example, this method can be overridden by any class that inherits it: Use the new modifier to explicitly hide a member inherited from a base class. To hide an inherited member, declare it in the derived class using the same name, and modify it with the new modifier.

这与多态性有关.当对引用调用虚拟方法时,引用所引用的对象的实际类型用于决定使用哪个方法实现.当基类的方法在派生类中被覆盖时,会使用派生类中的版本,即使调用代码不知道"该对象是派​​生类的实例.例如:

This is all to do with polymorphism. When a virtual method is called on a reference, the actual type of the object that the reference refers to is used to decide which method implementation to use. When a method of a base class is overridden in a derived class, the version in the derived class is used, even if the calling code didn't "know" that the object was an instance of the derived class. For instance:

public class Base
{
  public virtual void SomeMethod()
  {
  }
}

public class Derived : Base
{
  public override void SomeMethod()
  {
  }
}

...

Base d = new Derived();
d.SomeMethod();

如果覆盖 Base.SomeMethod,最终将调用 Derived.SomeMethod.

will end up calling Derived.SomeMethod if that overrides Base.SomeMethod.

现在,如果您使用 new 关键字而不是 override,派生类中的方法不会覆盖基类中的方法,它只是隐藏它.在这种情况下,代码如下:

Now, if you use the new keyword instead of override, the method in the derived class doesn't override the method in the base class, it merely hides it. In that case, code like this:

public class Base
{
  public virtual void SomeOtherMethod()
  {
  }
}

public class Derived : Base
{
  public new void SomeOtherMethod()
  {
  }
}

...


Base b = new Derived();
Derived d = new Derived();
b.SomeOtherMethod();
d.SomeOtherMethod();

将首先调用 Base.SomeOtherMethod ,然后是 Derived.SomeOtherMethod .它们实际上是两个完全独立的方法,它们碰巧具有相同的名称,而不是派生方法覆盖基本方法.

Will first call Base.SomeOtherMethod , then Derived.SomeOtherMethod . They're effectively two entirely separate methods which happen to have the same name, rather than the derived method overriding the base method.

如果您没有指定 new 或 overrides,则结果输出与您指定 new 的结果相同,但您还会收到编译器警告(因为您可能不知道您在其中隐藏了一个方法)基类方法,或者实际上您可能想要覆盖它,只是忘记包含关键字).

If you don't specify either new or overrides, the resulting output is the same as if you specified new, but you'll also get a compiler warning (as you may not be aware that you're hiding a method in the base class method, or indeed you may have wanted to override it, and merely forgot to include the keyword).

覆盖属性声明可能包含 sealed 修饰符.使用此修饰符可防止派生类进一步覆盖该属性.密封属性的访问器也是密封的.

An overriding property declaration may include the sealed modifier. Use of this modifier prevents a derived class from further overriding the property. The accessors of a sealed property are also sealed.

这篇关于virtual、override、new、sealed override的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆