多态性和继承

发布于 2024-12-11 11:06:24 字数 365 浏览 1 评论 0原文

我有这些类:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

痞味浪人 2024-12-18 11:06:24

多态性允许 B 型被视为 A 型。

A b = new B();
int x = b.Foo(); // x will be 1 if virtual, 5 if not.

Polymorphism allows type B to be treated as type A.

A b = new B();
int x = b.Foo(); // x will be 1 if virtual, 5 if not.
就像说晚安 2024-12-18 11:06:24

假设这是 C#:

您没有重写 Foo。您隐藏了基类的 Foo。这意味着,如果您使用静态类型 B 调用 foo 变量,您将得到 B.Foo() 并调用静态类型 A > (即使运行时类型为 B)将为您提供 A.Foo()

您的代码应该发出编译器警告,因为您在不使用 new 关键字的情况下隐藏了基本方法。

B b = new B();
int x = b.Foo();//calls B.Foo
A a = b;//Runtime type B, compiletime type A
a.Foo(); // calls A.Foo

如果您重写了 Foo,那么在这两种情况下您都会得到 B.Foo()

Assuming this is C#:

You didn't override Foo. You hid the Foo of the baseclass. This means that if you call foo an a variable with the static type B you will get B.Foo() and calling on the static type A (even if the type is B at runtime) will give you A.Foo().

Your code should give a compiler warning, since you're hiding the base method without using the new keyword.

B b = new B();
int x = b.Foo();//calls B.Foo
A a = b;//Runtime type B, compiletime type A
a.Foo(); // calls A.Foo

If you had overridden Foo then you'd get B.Foo() in both cases.

甜妞爱困 2024-12-18 11:06:24

您的声明类型是 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 ..

っ〆星空下的拥抱 2024-12-18 11:06:24

将方法标记为虚拟可以使其多态。这意味着它可以在派生类中被重写。当您从基类引用调用多态方法时,它就会发挥作用。将在运行时调用的方法取决于基类引用所指向的对象的运行时类。如果它的运行时类型是基类本身,则调用的方法将是基类上的方法;如果运行时类型是已重写该方法的派生类型,则调用的方法将是该重写的方法。

如果该方法不是虚拟,则它将绑定调用在对象的编译时引用类型中声明的方法,而不管其运行时类型如何。

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.

冷夜 2024-12-18 11:06:24

使方法成为虚拟的要点是当您通过基类指针或引用调用该方法时:

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 is B. This means that, due to polymorphism, B.Foo is called although the static type is A.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文