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

源码网商城

c#继承中的函数调用实例

  • 时间:2020-07-21 17:40 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:c#继承中的函数调用实例
本文实例讲述了c#继承中的函数调用方法,分享给大家供大家参考。具体分析如下: 首先看下面的代码:
[u]复制代码[/u] 代码如下:
using System;   namespace Test {     public class Base     {         public void Print()         {             Console.WriteLine(Operate(8, 4));         }           protected virtual int Operate(int x, int y)         {             return x + y;         }     } } namespace Test {     public class OnceChild : Base     {         protected override int Operate(int x, int y)         {             return x - y;         }     } } namespace Test {     public class TwiceChild : OnceChild     {         protected override int Operate(int x, int y)         {             return x * y;         }     } } namespace Test {     public class ThirdChild : TwiceChild     {     } } namespace Test {     public class ForthChild : ThirdChild     {         protected new int Operate(int x, int y)         {             return x / y;         }     } } namespace Test {     class Program     {         static void Main(string[] args)         {             Base b = null;             b = new Base();             b.Print();             b = new OnceChild();             b.Print();             b = new TwiceChild();             b.Print();             b = new ThirdChild();             b.Print();             b = new ForthChild();             b.Print();         }     } }
运行结果为: 12 4 32 32 32 从结果中可以看出:使用override重写之后,调用的函数是派生的最远的那个函数,使用new重写则是调用new之前的派生的最远的函数,即把new看做没有重写似的。 希望本文所述对大家的C#程序设计有所帮助。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部