“as”会启用多态性吗?将继承的类传递给采用基类启用多态性的方法?

发布于 2024-12-05 07:37:36 字数 318 浏览 0 评论 0原文

首先,我将使用virtualoverride

例如,基类A有方法A.do(),继承类B有< code>B.do() 覆盖 A 的。

如果我调用(B as A).do(),它会执行哪个do()

或者,如果有一个方法 void mymethod(A a) {a.do()},现在我通过 B b 调用它; mymethod(b),它会执行b.do()吗?

Fist of all, I will use virtual and override

for example, base class A has method A.do(), inherited class B has B.do() which overrides A's.

if I call (B as A).do(), which do() would it execute?

or, if there is a method void mymethod(A a) {a.do()}, now I call it by B b; mymethod(b), would it execute b.do()?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

念﹏祤嫣 2024-12-12 07:37:36

最顶层的重写方法总是会被调用,即 b.Do()(b as A).Do()((A)b) .Do() 将调用 B.Do()

如果子类重写它,我不知道如何从子类调用基方法。

The most top override method always will be called, i.e. b.Do() or (b as A).Do() or ((A)b).Do() will call B.Do().

I don't know a way how to call a base method from child class if child class overrides it.

身边 2024-12-12 07:37:36
public class A
{
    public virtual void Do() { Console.Write("a"); }
    public void Do2() { Console.Write("a2"); }
}

public class B : A
{
    public override void Do() { Console.Write("b"); }
    public new void Do2() { Console.Write("b2"); }
}

class Program
{
    static void Main(string[] args)
    {
        B b = new B();
        A a = b;
        b.Do();               //b
        ( b as A ).Do();      //b
        a.Do();               //b
        ( (A)b ).Do();        //b

        ( b as A ).Do2();     //a2
        ( (A)b ).Do2();       //a2
        ( b ).Do2();          //b2
    }
}  

输出:

b b b b
a2 a2 b2
public class A
{
    public virtual void Do() { Console.Write("a"); }
    public void Do2() { Console.Write("a2"); }
}

public class B : A
{
    public override void Do() { Console.Write("b"); }
    public new void Do2() { Console.Write("b2"); }
}

class Program
{
    static void Main(string[] args)
    {
        B b = new B();
        A a = b;
        b.Do();               //b
        ( b as A ).Do();      //b
        a.Do();               //b
        ( (A)b ).Do();        //b

        ( b as A ).Do2();     //a2
        ( (A)b ).Do2();       //a2
        ( b ).Do2();          //b2
    }
}  

Output:

b b b b
a2 a2 b2
明媚如初 2024-12-12 07:37:36

这完全取决于 do() 方法是否被声明为虚拟方法。如果它不是虚拟的,则调用 A.do()。如果它是虚拟的,则调用 B.do()。 virtual 关键字启用了多态性,并允许调用独立于引用类型的方法。

C# 中没有任何机制允许从 B 对象引用直接调用虚拟 A.do() 方法。唯一的例外是在类 B 的实例方法中使用 base.do()。

It entirely depends on whether the do() method was declared virtual or not. If it is not virtual then A.do() is called. If it is virtual then B.do() is called. It is the virtual keyword that enables polymorphism and allows calling a method independent of the type of the reference.

There is no mechanism in C# that allows directly calling a virtual A.do() method from a B object reference. The only exception is using base.do() inside an instance method of class B.

比忠 2024-12-12 07:37:36
public class A
{
    public A() { }
    public void Do() { Console.Write("A"); }

}

public class B : A
{
    public B() { }
    public void Do() { Console.Write("B");  }
}

class Program
{
    static void Main(string[] args)
    {
        B b = new B();
        b.Do(); //<-- outputs B
        (b as A).Do(); //<-- outputs A
      }
}

编译器警告隐藏不覆盖

警告 1“ConsoleApplication5.B.Do()”隐藏继承成员
'ConsoleApplication5.A.Do()'。如果需要隐藏,请使用 new 关键字
故意的。 c:\Program.cs 18 21 ConsoleApplication5

这是因为您没有重写任何内容,而只是隐藏 A 的方法。

但是

public class A
{
    public A() { }
    public virtual void Do() { Console.Write("A"); }

}

public class B : A
{
    public B() { }
    public  override void Do() { Console.Write("B");  }
}

在方法被重写时调用 B 两次。

public class A
{
    public A() { }
    public void Do() { Console.Write("A"); }

}

public class B : A
{
    public B() { }
    public void Do() { Console.Write("B");  }
}

class Program
{
    static void Main(string[] args)
    {
        B b = new B();
        b.Do(); //<-- outputs B
        (b as A).Do(); //<-- outputs A
      }
}

compiler warns for hiding not overriding:

Warning 1 'ConsoleApplication5.B.Do()' hides inherited member
'ConsoleApplication5.A.Do()'. Use the new keyword if hiding was
intended. c:\Program.cs 18 21 ConsoleApplication5

that is since you are not overriding anything, but simply hiding the method from A.

however

public class A
{
    public A() { }
    public virtual void Do() { Console.Write("A"); }

}

public class B : A
{
    public B() { }
    public  override void Do() { Console.Write("B");  }
}

calls B twice, when method is overridden.

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