我的c#--类的继承,事件归纳
现在想写些基础的东西,方便自己整理总结:
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5
6namespace testClass
7{
8 public class Program
9 {
10 static void Main(string[] args)
11 {
12 ParentClass parent = new ParentClass();
13 ParentClass childToParent = new ChildClass();
14 ChildClass child = new ChildClass();
15 Console.WriteLine("基类->基类对象 ParentClass parent = new ParentClass(); parent成员a={0}", parent.a);//输出3
16 Console.WriteLine("子类->基类对象\r\n ParentClass childToParent = new ChildClass(); childToParent成员a={0}", childToParent.a);//3
17 Console.WriteLine("子类->子类对象 ChildClass child = new ChildClass(); child成员a{0}", child.a);//322
18
19 parent.test();//c.test 基类->基类对象
20 childToParent.test();//c.test 子类->基类对象,用override 则调用之类方法,输出ChildClass.test
21 child.test();//ChildClass.test 子类->子类对象
22
23 Console.WriteLine("访问 基类 静态成员 stat={0}", ParentClass.stat);//300
24 Console.WriteLine("访问 子类 静态成员 stat={0}", ChildClass.stat);//30011
25 ParentClass.teststatic();
26 ChildClass.teststatic();
27
28 Console.WriteLine("基类成员包含 基类->基类对象 子类->基类对象 子类->子类对象 ,都必须是静态的\r\n{0}", ParentClass.pInp.a);//3
29 Console.WriteLine("{0}", ParentClass.pInp_child.a);//322
30 Console.WriteLine("{0}", ParentClass.cinparent.a);//3
31 ParentClass.cinparent.test();//ChildClass.test
32 Console.WriteLine("子类成员包含 基类->基类对象 子类->基类对象 子类->子类对象 ,都必须是静态的\r\n{0}", ChildClass.pInp.a);//3
33 Console.WriteLine("{0}", ChildClass.pInp_child.a);//322
34 Console.WriteLine("{0}", ChildClass.cinparent.a);//3
35 ChildClass.cinparent.test();//ChildClass.test
36 /**********委托**********/
37 Console.WriteLine("\r\n/**********委托**********/");
38 ParentClass.MathsOp mo = new ParentClass.MathsOp(parent.mydeleget);
39 Console.WriteLine("调用基类的委托MathsOp,委托参数用基类的mydeleget;结果{0}", mo(36.5));//36.5
40 ParentClass.MathsOp moMain = new ParentClass.MathsOp(mydeleget);
41 Console.WriteLine("调用基类的委托MathsOp,委托参数用Program.Main的mydeleget;结果{0}", moMain(141.88));//141.88
42 Console.WriteLine("/**********委托 end**********/");
43 /*委托 end*/
44 //订阅事件 类的静态事件也是可以的
45 //c.EventMathsOp += new c.MathsOp(c_EventMathsOp);
46 //c.testEvent();
47 Console.WriteLine("\r\n\r\n //订阅事件 类的静态事件也是可以的\r\n //ParentClass.EventMathsOp += new ParentClass.MathsOp(c_EventMathsOp);\r\n //ParentClass.testEvent();");
48 parent.EventMathsOp += new ParentClass.MathsOp(parent_EventMathsOp);
49 parent.testEvent();
50 Console.ReadLine();
51
52 }
53
54 static double ParentClass_EventMathsOp(double x)
55 {
56 Console.WriteLine("你调用的MathsOp事件,值是{0}", x);
57 return x;
58 }
59
60 static double parent_EventMathsOp(double x)
61 {
62 Console.WriteLine("你调用的MathsOp事件,值是{0}",x);
63 return x;
64 }
65 public static double mydeleget(double x)
66 {
67 return x;
68 }
69
70 }
71
72 public class ParentClass
73 {
74 public int a=3 ;
75 public static int stat = 300;
76 public static ParentClass pInp = new ParentClass();
77 public static ChildClass pInp_child = new ChildClass();
78 public static ParentClass cinparent = new ChildClass();
79 public ParentClass()
80 {
81 //pInp = new c();
82 }
83 public virtual void test()
84 {
85 Console.WriteLine("你调用的是基类ParentClass.test");
86 }
87 public static void teststatic()
88 {
89 Console.WriteLine("你调用的是 基类 静态方法ParentClass.test_static");
90 }
91 public delegate double MathsOp(double x);
92 public double mydeleget(double x)
93 {
94 return x;
95 }
96 public static double mydelegetStatic(double x)
97 {
98 return x;
99 }
100 public /* static*/ event MathsOp EventMathsOp = new MathsOp(mydelegetStatic);
101 public /* static */ void testEvent()
102 {
103 double x=new double();
104 x=(double) Console.Read();
105 while ( x!= 0.0)
106 {
107 EventMathsOp(x);
108 x = (double)Console.Read();
109 }
110
111 }
112 }
113 public class ChildClass : ParentClass
114 {
115 public static ParentClass pInp = new ParentClass();
116 public static ChildClass pInp_child = new ChildClass();
117 public static ParentClass cinparent = new ChildClass();
118
119 public int a = 322;
120 public static int stat = 30011;
121 public override void test()
122 {
123 Console.WriteLine("你调用的是 子类 ChildClass.test,这里用了override重写test()方法");
124 }
125
126 public static void teststatic()
127 {
128 Console.WriteLine("你调用的是 子类 静态方法ChildClass.test_static");
129 }
130 }
131}
132
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5
6namespace testClass
7{
8 public class Program
9 {
10 static void Main(string[] args)
11 {
12 ParentClass parent = new ParentClass();
13 ParentClass childToParent = new ChildClass();
14 ChildClass child = new ChildClass();
15 Console.WriteLine("基类->基类对象 ParentClass parent = new ParentClass(); parent成员a={0}", parent.a);//输出3
16 Console.WriteLine("子类->基类对象\r\n ParentClass childToParent = new ChildClass(); childToParent成员a={0}", childToParent.a);//3
17 Console.WriteLine("子类->子类对象 ChildClass child = new ChildClass(); child成员a{0}", child.a);//322
18
19 parent.test();//c.test 基类->基类对象
20 childToParent.test();//c.test 子类->基类对象,用override 则调用之类方法,输出ChildClass.test
21 child.test();//ChildClass.test 子类->子类对象
22
23 Console.WriteLine("访问 基类 静态成员 stat={0}", ParentClass.stat);//300
24 Console.WriteLine("访问 子类 静态成员 stat={0}", ChildClass.stat);//30011
25 ParentClass.teststatic();
26 ChildClass.teststatic();
27
28 Console.WriteLine("基类成员包含 基类->基类对象 子类->基类对象 子类->子类对象 ,都必须是静态的\r\n{0}", ParentClass.pInp.a);//3
29 Console.WriteLine("{0}", ParentClass.pInp_child.a);//322
30 Console.WriteLine("{0}", ParentClass.cinparent.a);//3
31 ParentClass.cinparent.test();//ChildClass.test
32 Console.WriteLine("子类成员包含 基类->基类对象 子类->基类对象 子类->子类对象 ,都必须是静态的\r\n{0}", ChildClass.pInp.a);//3
33 Console.WriteLine("{0}", ChildClass.pInp_child.a);//322
34 Console.WriteLine("{0}", ChildClass.cinparent.a);//3
35 ChildClass.cinparent.test();//ChildClass.test
36 /**********委托**********/
37 Console.WriteLine("\r\n/**********委托**********/");
38 ParentClass.MathsOp mo = new ParentClass.MathsOp(parent.mydeleget);
39 Console.WriteLine("调用基类的委托MathsOp,委托参数用基类的mydeleget;结果{0}", mo(36.5));//36.5
40 ParentClass.MathsOp moMain = new ParentClass.MathsOp(mydeleget);
41 Console.WriteLine("调用基类的委托MathsOp,委托参数用Program.Main的mydeleget;结果{0}", moMain(141.88));//141.88
42 Console.WriteLine("/**********委托 end**********/");
43 /*委托 end*/
44 //订阅事件 类的静态事件也是可以的
45 //c.EventMathsOp += new c.MathsOp(c_EventMathsOp);
46 //c.testEvent();
47 Console.WriteLine("\r\n\r\n //订阅事件 类的静态事件也是可以的\r\n //ParentClass.EventMathsOp += new ParentClass.MathsOp(c_EventMathsOp);\r\n //ParentClass.testEvent();");
48 parent.EventMathsOp += new ParentClass.MathsOp(parent_EventMathsOp);
49 parent.testEvent();
50 Console.ReadLine();
51
52 }
53
54 static double ParentClass_EventMathsOp(double x)
55 {
56 Console.WriteLine("你调用的MathsOp事件,值是{0}", x);
57 return x;
58 }
59
60 static double parent_EventMathsOp(double x)
61 {
62 Console.WriteLine("你调用的MathsOp事件,值是{0}",x);
63 return x;
64 }
65 public static double mydeleget(double x)
66 {
67 return x;
68 }
69
70 }
71
72 public class ParentClass
73 {
74 public int a=3 ;
75 public static int stat = 300;
76 public static ParentClass pInp = new ParentClass();
77 public static ChildClass pInp_child = new ChildClass();
78 public static ParentClass cinparent = new ChildClass();
79 public ParentClass()
80 {
81 //pInp = new c();
82 }
83 public virtual void test()
84 {
85 Console.WriteLine("你调用的是基类ParentClass.test");
86 }
87 public static void teststatic()
88 {
89 Console.WriteLine("你调用的是 基类 静态方法ParentClass.test_static");
90 }
91 public delegate double MathsOp(double x);
92 public double mydeleget(double x)
93 {
94 return x;
95 }
96 public static double mydelegetStatic(double x)
97 {
98 return x;
99 }
100 public /* static*/ event MathsOp EventMathsOp = new MathsOp(mydelegetStatic);
101 public /* static */ void testEvent()
102 {
103 double x=new double();
104 x=(double) Console.Read();
105 while ( x!= 0.0)
106 {
107 EventMathsOp(x);
108 x = (double)Console.Read();
109 }
110
111 }
112 }
113 public class ChildClass : ParentClass
114 {
115 public static ParentClass pInp = new ParentClass();
116 public static ChildClass pInp_child = new ChildClass();
117 public static ParentClass cinparent = new ChildClass();
118
119 public int a = 322;
120 public static int stat = 30011;
121 public override void test()
122 {
123 Console.WriteLine("你调用的是 子类 ChildClass.test,这里用了override重写test()方法");
124 }
125
126 public static void teststatic()
127 {
128 Console.WriteLine("你调用的是 子类 静态方法ChildClass.test_static");
129 }
130 }
131}
132