继承中的新,虚拟,新虚拟和覆盖之间有什么不同? [英] what is different between new , virtual , new virtual ,and override in inheritance ?

查看:41
本文介绍了继承中的新,虚拟,新虚拟和覆盖之间有什么不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

class Base
{
    public new virtual void Show()
    {
        Console.WriteLine("Base ");
    }
}
class Child1 : Base
{
    public override void Show()
    {
        Console.WriteLine("Drived1 ");
    }
}
class Child2 : Child1
{
    public virtual void Show()
    {
        Console.WriteLine("Drived2 ");
    }
}
class Child3 : Child1
{
    public new void Show()
    {
        Console.WriteLine("Drived3 ");
    }
}

推荐答案

1。新的虚拟 - 这种组合没有意义(见下文)。

2.覆盖 - 你重写(引入新的实现)一些基本的虚拟或抽象方法

3 .virtual - 这个方法可以在派生(子)类中覆盖

5. new - 你介绍方法的新实现,没有标记虚拟关键字
1. new virtual - this combination does not make sense (see below).
2. override - you are overriding (introducing new implementation of) some base virtual or abstract method
3. virtual - this method can be overidden in derived (child) class
5. new - you are introdicing new implementation of method, which was not marked with virtual keyword


这篇关于继承中的新,虚拟,新虚拟和覆盖之间有什么不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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