1、虚方法练习
设计一个控制台应用程序,定义一个Shape类,具体要求如下: (1)类中定义2个私有字段长度(length)、宽度(breadth)。 (2)类中定义相应公有属性分别对应上述2个字段; (3)类中定义可重载的构造函数初始化上述2个字段; (4)定义1个默认构造函数; (5)类中定义公有方法输出对象的长度、宽度等详细信息; (6)类中定义虚方法Draw,输出当前图型类别。 (7)在main方法中测试Shape类及方法。 定义一个Box类,父类为Shape,具体要求如下: (1)类中定义3个私有字段长度(length)、宽度(breadth)、高度(height); (2)类中定义相应公有属性分别对应上述3个字段; (3)类中定义可重载的构造函数初始化上述3个字段; (4)定义1个默认构造函数; (5)类中定义公有方法输出对象的长度、宽度、高度等详细信息; (6)类中定义虚方法Draw,输出当前图型类别。 (7)在main方法中测试Box类及方法; (8)在main方法中测试多态。
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace MyProject 7 { 8 class Program 9 { 10 static void Main(string[] args) 11 { 12 #region 测试Shape类及方法 13 /* 1、默认构造函数 对Length,breadth进行赋值 14 * 2、利用属性进行输出后,再利用属性进行赋值 15 * 3、最后利用ToString 输出对应的对象信息 16 */ 17 #endregion 18 19 Shape s1 = new Shape(1, 2); 20 Console.WriteLine("s1->Length:{0} , breadth:{1}",s1.Length,s1.Breadth); 21 s1.Length = 3; 22 s1.Breadth = 4; 23 Console.WriteLine(s1); 24 25 #region 测试Box类及方法 26 /* 1、默认构造函数 对Length,breadth,height进行赋值 27 * 2、利用属性进行输出后,再利用属性进行赋值 28 * 3、最后利用ToString 输出对应的对象信息 29 */ 30 #endregion 31 32 Box b1 = new Box(1, 2, 3); 33 Console.WriteLine("b1->Length :{0}\tbreadth :{1}\theight :{2}",b1.Length,b1.Breadth,b1.Height); 34 b1.Length = 3; 35 b1.Breadth = 4; 36 b1.Height = 5; 37 Console.WriteLine(b1); 38 39 #region 测试多态 40 /* 41 * 多态体现在,父指针,将子类实例化. 42 */ 43 #endregion 44 Shape b2 = new Box(2, 3, 4); 45 Console.WriteLine(b2); 46 47 48 #region 测试虚函数 49 #endregion 50 s1.Draw(); 51 b1.Draw(); 52 } 53 } 54 55 public class Shape 56 { 57 private int length; 58 private int breadth; 59 60 public int Length 61 { 62 get { return length; } 63 set { length = value; } 64 } 65 public int Breadth 66 { 67 get { return breadth; } 68 set { breadth = value; } 69 } 70 71 public Shape ( int length , int width) 72 { 73 this.Length = length; 74 this.Breadth = Breadth; 75 } 76 public Shape():this(0, 0) { } 77 public override string ToString() 78 { 79 return string.Format("Shape -> length:{0}\t Breadth:{1}", Length, Breadth); 80 } 81 public virtual void Draw() 82 { 83 Console.WriteLine(" Draw -> Shape " + "Length :{0} , Breadth :{1} ",Length , Breadth ); 84 } 85 #region 主要注释 86 /* 87 * 类中的字段属性封装 88 * 构造函数 : 两参数,零参数 89 * 重载ToString类 90 */ 91 #endregion 92 } 93 94 public class Box : Shape 95 { 96 private int height; 97 98 public int Height 99 { 100 get { return height; } 101 set { height = value; } 102 } 103 104 public Box(int length, int Breadth, int height):base(length, Breadth) 105 { 106 this.height = height; 107 } 108 public Box() : this(0, 0, 0) { } 109 public override string ToString() 110 { 111 return string.Format("Box -> length:{0}\t breadth:{1}\t height:{2}", Length, Breadth, Height); 112 } 113 public override void Draw() 114 { 115 Console.WriteLine(" Draw -> Shape " + "Length :{0} , Breadth :{1} , Height :{2}", Length, Breadth,Height); 116 } 117 #region Box类 118 /* 119 * Box继承了Shape的所有结构 120 * 121 * 1、无法直接访问父类的字段,但可通过"属性"间接访问 122 * 2、在重载 构造函数时,可以直接调用父类的构造函数实现部分字段的赋值 123 * 124 */ 125 #endregion 126 } 127 }