手机
当前位置:查字典教程网 >编程开发 >C#教程 >重写、隐藏基类(new, override)的方法
重写、隐藏基类(new, override)的方法
摘要:复制代码代码如下:publicclassFather{publicvoidWrite(){Console.WriteLine("父");}}...

复制代码 代码如下:

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("女");

}

}

复制代码 代码如下:

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();

}

输出:

添加调用父方法:

复制代码 代码如下:

public class Boy : Father

{

public new void Write()

{

base.Write();

Console.WriteLine("子");

}

}

public class Girl : Mother

{

public override void Write()

{

base.Write();

Console.WriteLine("女");

}

}

输出:

可见,在程序运行结果上new 和override是一样的。

【重写、隐藏基类(new, override)的方法】相关文章:

C# 大数据导出word的假死报错的处理方法

C#TreeView 无限级别分类实现方法

c# 对cookies(增、删、改、查)的操作方法

自定义实现Json字符串向C#对象转变的方法

C#实现对AES加密和解密的方法

c#中分割字符串的几种方法

用C#编写获取远程IP,MAC的方法

C# 语音功能的实现方法

c#实现隐藏与显示任务栏的方法详解

使用SmtpClient发送邮件的方法

精品推荐
分类导航