国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 特克斯县| 定边县| 桑植县| 齐河县| 吉林市| 铜山县| 济阳县| 儋州市| 博客| 武平县| 将乐县| 盐边县| 甘谷县| 博客| 曲靖市| 中山市| 敦化市| 泾阳县| 临武县| 乐东| 聊城市| 汉源县| 沙湾县| 永德县| 七台河市| 竹山县| 大关县| 文化| 徐闻县| 喜德县| 龙南县| 夏津县| 陵水| 新泰市| 尤溪县| 江川县| 红原县| 玉门市| 普洱| 东乡| 澄迈县|