源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

重写、隐藏基类(new, override)的方法

  • 时间:2020-01-11 15:04 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:重写、隐藏基类(new, override)的方法
[u]复制代码[/u] 代码如下:
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("女");         }     }
[u]复制代码[/u] 代码如下:
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();         }
输出: 父 子 母 女 添加调用父方法:
[u]复制代码[/u] 代码如下:
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是一样的。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部