多态性和继承
我有这些类:
class A
{
public int Foo()
{
return 5;
}
}
class B : A
{
public int Foo()
{
return 1;
}
}
并且我像这样使用它们:
B b = new B();
int x = b.Foo();
虽然基类中的 Foo() 不是虚拟的,或者在派生类中 - 它没有 override 关键字,但 x 仍然等于 1。那么,什么是使用 virtual 和 override 关键字?
I have these classes:
class A
{
public int Foo()
{
return 5;
}
}
class B : A
{
public int Foo()
{
return 1;
}
}
and I use them like this:
B b = new B();
int x = b.Foo();
and although Foo() in the base class isn't virtual, or in the derived class - it hasn't the override keyword, still x equals 1. Then, what is the use of the virtual and override keywords?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
多态性允许 B 型被视为 A 型。
Polymorphism allows type B to be treated as type A.
假设这是 C#:
您没有重写
Foo
。您隐藏了基类的Foo
。这意味着,如果您使用静态类型B
调用 foo 变量,您将得到B.Foo()
并调用静态类型A
> (即使运行时类型为B
)将为您提供A.Foo()
。您的代码应该发出编译器警告,因为您在不使用
new
关键字的情况下隐藏了基本方法。如果您重写了
Foo
,那么在这两种情况下您都会得到B.Foo()
。Assuming this is C#:
You didn't override
Foo
. You hid theFoo
of the baseclass. This means that if you call foo an a variable with the static typeB
you will getB.Foo()
and calling on the static typeA
(even if the type isB
at runtime) will give youA.Foo()
.Your code should give a compiler warning, since you're hiding the base method without using the
new
keyword.If you had overridden
Foo
then you'd getB.Foo()
in both cases.您的声明类型是 B,因此您将调用 B 的实现..如果您的声明类型是 A 并且您正在实例化 B 的新实例,那么您将得到 5 作为声明类型,除非您使用 virtual 关键字来支持多态性..
Your declaring type is B thus you will call the implementation of B.. if your declaring type is A and you are instantiating new instance of B then you get 5 as the declaring type unless you use virtual keyword to support polymorphysm ..
将方法标记为
虚拟
可以使其多态。这意味着它可以在派生类中被重写。当您从基类引用调用多态方法时,它就会发挥作用。将在运行时调用的方法取决于基类引用所指向的对象的运行时类。如果它的运行时类型是基类本身,则调用的方法将是基类上的方法;如果运行时类型是已重写该方法的派生类型,则调用的方法将是该重写的方法。如果该方法不是
虚拟
,则它将绑定调用在对象的编译时引用类型中声明的方法,而不管其运行时类型如何。Marking a method as
virtual
allows it to be polymorphic. That means that it can be overridden in derived classes. A polymorphic method comes into play when you call it from a base class reference. The method that's going to get called at runtime depends on the runtime class of the object pointed by the base class reference. If its runtime type is the base class itself, the method called will be the one on the base class; if the runtime type is of a derived type that has overridden that method, the method called will be that overridden method.If the method is not
virtual
, it will be bound to call the method that's declared in the type of the compile time reference to the object, regardless of its runtime type.使方法成为虚拟的要点是当您通过基类指针或引用调用该方法时:
A p = new B();
int x = p.Foo();
在此示例中,p 的静态类型为
A
,但动态类型为B
。这意味着,由于多态性,尽管静态类型为A
,但仍会调用B.Foo
。The point of making the method virtual is when you call the method via a base class pointer or reference:
A p = new B();
int x = p.Foo();
In this example, the static type of p is
A
, but the dynamic type isB
. This means that, due to polymorphism,B.Foo
is called although the static type isA
.