通过基类调用子类中override 、 new 方法和构造函数的表现

  1. /*
  2.  * Created by SharpDevelop.
  3.  * User: Administrator
  4.  * Date: 2008-9-8
  5.  * Time: 8:24
  6.  * 
  7.  * To change this template use Tools | Options | Coding | Edit Standard Headers.
  8.  */
  9. using System;
  10. namespace TestNewMethodandOverride
  11. {
  12.     class Shape
  13.     {
  14.         public Shape()
  15.         {
  16.             Console.WriteLine("Shape.Shape() is called");
  17.         }
  18.         public Shape(int r)
  19.         {
  20.             Console.WriteLine("Shape.Shape(int) is called");
  21.         }       
  22.         public  virtual void Draw()
  23.         {
  24.             Console.WriteLine("Shape.Draw() is called");
  25.         }
  26.     }
  27.     class Circle:Shape
  28.     {
  29.         public Circle()
  30.         {
  31.             Console.WriteLine("Circle.Circle() is called");
  32.         }
  33.         public Circle(int r)
  34.         {
  35.             Console.WriteLine("Circle.Circle(int) is called");
  36.         }
  37.         public  override void Draw()
  38.         {
  39.             Console.WriteLine("Circle.Draw() is called");
  40.         }
  41.     }
  42.     class Rect:Shape
  43.     {
  44.         public Rect()
  45.         {
  46.             Console.WriteLine("Rect.Rect() is called");
  47.         }
  48.         public Rect(int r):base(r)
  49.         {
  50.             Console.WriteLine("Rect.Rect(int) is called");
  51.         }
  52.         public  override void Draw()
  53.         {
  54.             Console.WriteLine("Rect.Draw() is called");
  55.         }
  56.     }
  57.     class Strange:Shape
  58.     {
  59.         public Strange()
  60.         {
  61.             Console.WriteLine("Strange.Strange() is called");
  62.         }
  63.         public  new void Draw()
  64.         {
  65.             Console.WriteLine("Strange.Draw() is called");
  66.         }
  67.     }
  68.     class Program
  69.     {
  70.         public static void Main(string[] args)
  71.         {
  72.             Console.WriteLine("===new()===");
  73.             //在子类默认构造函数被调用前父类的默认构造函数首先被调用
  74.             Shape [] shape={new Circle(),new Rect(),new Strange()};
  75.             
  76.             //子类重载的方法和基类方法同一入口即基类方法“消失”了
  77.             Console.WriteLine("===Draw()===");
  78.             foreach(Shape i in shape)
  79.                 i.Draw();
  80.             Console.WriteLine("===Strange===");
  81.             //但是new方法例外,他和基类方法只是同名而不是同一入口
  82.             //两个同名方法需要显式的调用
  83.             Strange f = new Strange();
  84.             f.Draw();
  85.             ((Shape) f).Draw();
  86.             Console.WriteLine("===Circle===");
  87.             //在子类自定义构造函数被调用前父类的默认构造函数首先被调用
  88.             //public Circle(int r)
  89.             Circle c =new Circle(10);
  90.             c.Draw();
  91.             //通过基类调用方法(重载的)仍能正确映射到子类方法
  92.             ((Shape) c).Draw();
  93.             Console.WriteLine("===Rect===");
  94.             //在子类自定义构造函数被调用前父类的默认构造函数首先被调用
  95.             //除非子类自定义构造函数显示的调用基类自定义构造函数
  96.             //public Cect(int r):base(r)
  97.             Rect r =new Rect(10);
  98.             r.Draw();
  99.             //通过基类调用方法(重载的)仍能正确映射到子类方法
  100.             ((Shape) r).Draw();
  101.             
  102.             Console.WriteLine("============");
  103.             
  104.             Console.Write("Press any key to continue . . . ");
  105.             Console.ReadKey(true);
  106.         }
  107.     }
  108. }

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值