虚拟的,覆盖新的密封覆盖的区别 [英] The difference between virtual, override, new and sealed override

查看:116
本文介绍了虚拟的,覆盖新的密封覆盖的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是OOP的一些概念混淆之间pretty:虚拟覆盖封控。任何人能解释的区别?

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

我是pretty明确表示,如果派生类的方法被使用,可以使用覆盖关键字,这样基类方法将被重写派生类。但我不知道封控

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.

推荐答案

虚拟关键字用于修改方法,属性,索引器或事件声明,并允许它在派生覆盖类。例如,这种方法可以通过继承它的类重写:
使用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.

这是所有做多态。当虚拟方法被调用的一个参考,实际类型的对象,提及是指用于决定使用哪种方法实现。当一个基类的方法在派生类中被覆盖,在派生类中使用的版本,即使调用code并不知道的对象是派生类的一个实例。例如:

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();

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

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

现在,如果您使用关键字来代替替换后,在派生类中的方法不会覆盖在基类中的方法,它只是隐藏它。在这种情况下,code是这样的:

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.

如果你不指定新的或重写,因为如果你指定新的输出结果是一样的,但你也将获得一个编译器警告(你可能不知道,你隐藏了一种方法,基类方法,或者实际上你可能想覆盖它,而只是忘了,包括关键字)。

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).

这是重写属性声明可能包括在密封修改。该修改$ P $的用途进一步重写属性pvents派生类。密封属性的访问器也是密封的。

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.

这篇关于虚拟的,覆盖新的密封覆盖的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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