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 }
49
50 public class Shape
51 {
52 private int length;
53 private int breadth;
54
55 public int Length { get => length; set => length = value; }
56 public int Breadth { get => breadth; set => breadth = value; }
57
58 public Shape ( int length , int width)
59 {
60 this.Length = length;
61 this.Breadth = Breadth;
62 }
63 public Shape():this(0, 0) { }
64 public override string ToString()
65 {
66 return string.Format("Shape -> length:{0}\t Breadth:{1}", length, Breadth);
67 }
68 #region 主要注释
69 /*
70 * 类中的字段属性封装
71 * 构造函数 : 两参数,零参数
72 * 重载ToString类
73 */
74 #endregion
75 }
76
77 public class Box : Shape
78 {
79 private int height;
80 public int Height { get => height; set => height = value; }
81 public Box(int length, int Breadth, int height):base(length, Breadth)
82 {
83 this.height = height;
84 }
85 public Box() : this(0, 0, 0) { }
86 public override string ToString()
87 {
88 return string.Format("Box -> length:{0}\t breadth:{1}\t height:{2}", Length, Breadth, height);
89 }
90 #region Box类
91 /*
92 * Box继承了Shape的所有结构
93 *
94 * 1、无法直接访问父类的字段,但可通过"属性"间接访问
95 * 2、在重载 构造函数时,可以直接调用父类的构造函数实现部分字段的赋值
96 *
97 */
98 #endregion
99 }
100 }