子类与父类的小关系:盒子套盒子

  1. class Program   
  2. {   
  3.     static void Main(string[] args)   
  4.     {   
  5.         InBox son = new InBox();   
  6.         son.m1();   
  7.         son.m2();   
  8.         Console.WriteLine("转换成父类");   
  9.         Box box = (Box)son;   
  10.         box.m1();     //从这儿就能看出,只有父类的方法,而没有子类的方法了?所以像不像是父类就是子类里的一个盒子?   
  11.     }   
  12. }   
  13.   
  14.   
  15. class Box   
  16. {   
  17.     public void m1()   
  18.     {   
  19.         Console.WriteLine("我是父类的方法");   
  20.         Console.ReadLine();   
  21.     }   
  22. }   
  23.   
  24. class InBox : Box   
  25. {   
  26.     public void m2()   
  27.     {   
  28.         Console.WriteLine("我是子类的方法");   
  29.         Console.ReadLine();   
  30.     }   
  31. }  
原文地址:https://www.cnblogs.com/leeolevis/p/1486079.html