国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

重寫、隱藏基類(new, override)的方法

2020-01-24 03:34:50
字體:
供稿:網(wǎng)友

復(fù)制代碼 代碼如下:

public class Father
    {
        public void Write() {
            Console.WriteLine("父");
        }
    }

    public class Mother
    {
        public virtual void Write()
        {
            Console.WriteLine("母");
        }
    }

    public class Boy : Father
    {
        public new void Write()
        {
            Console.WriteLine("子");
        }
    }

    public class Girl : Mother
    {
        public override void Write()
        {
            Console.WriteLine("女");
        }
    }


復(fù)制代碼 代碼如下:

static void Main(string[] args)
        {
            Father father = new Boy();
            father.Write();

            Boy boy = new Boy();
            boy.Write();


            Mother mother = new Mother();
            mother.Write();

            Girl girl = new Girl();
            girl.Write();

            Console.ReadLine();
        }


輸出:




添加調(diào)用父方法:

復(fù)制代碼 代碼如下:

public class Boy : Father
    {
        public new void Write()
        {
            base.Write();
            Console.WriteLine("子");
        }
    }

    public class Girl : Mother
    {
        public override void Write()
        {
            base.Write();
            Console.WriteLine("女");
        }
    }


輸出:






可見,在程序運(yùn)行結(jié)果上new 和override是一樣的。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 乌兰察布市| 揭阳市| 娄烦县| 将乐县| 财经| 禹州市| 河源市| 黑水县| 竹山县| 济源市| 裕民县| 阳信县| 深圳市| 庆安县| 阿荣旗| 禄劝| 明溪县| 东阿县| 库尔勒市| 河北区| 绥化市| 永安市| 遂溪县| 和田市| 喀喇| 全南县| 壶关县| 威宁| 吉林市| 日土县| 永州市| 攀枝花市| 永宁县| 营口市| 鄯善县| 靖西县| 乌什县| 贵溪市| 宁都县| 福海县| 安康市|