代码
1
using
System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Reflection;
6
7 namespace ConsoleTest
8 {
9 // 接口定义
10 interface IEmploy
11 {
12 // 继承该接口要实现的方法
13 void Speak( string s);
14 }
15
16 // 接口定义2
17 interface ISurface
18 {
19 void Speak( string s);
20 void Surface();
21 }
22
23
24 // Hello类实现接口,继承了2个接口
25 public class Hello : IEmploy, ISurface
26 {
27 // 实现接口1方法
28 public void Speak( string s) // 实现方法
29 {
30 Console.WriteLine( " Hello " + s);
31 }
32 // 实现接口2方法
33 public void Surface()
34 {
35 Console.WriteLine( " Test " );
36 }
37 // 实现方法2的Speak方法,为了避免编译冲突,必须使用显式实现
38 void ISurface.Speak( string s)
39 {
40 Console.WriteLine( " ISurface.Speak: " + s);
41 }
42 }
43
44 // Sorry类实现接口的方法
45 public class Sorry : IEmploy
46 {
47 public void Speak( string s)
48 {
49 Console.WriteLine( " Sorry " + s);
50 }
51 }
52
53 /// <summary>
54 /// 直接实例化使用
55 /// </summary>
56 public class bll
57 {
58 /* **********
59 1.类成员继承了IEmploy 只能通过 IEmploy 接口使用Speak方法
60 比如,不能如下的方法使用:
61 Hello h1 = new Hello();
62 h1.Speak("直接调用,hello类");
63
64 2.IEmploy,ISurface 接口的两个方法实现都是分离的,都不可以直接在类中使用
65
66 3.有个我不太理解,我以为下面是可以的,但是却不行
67 Hello H1=new Hello();
68 IEmploy test=(IEmploy)H1;
69 编译提示错误A field initializer cannot reference the non-static field, method, or property 'ConsoleTest.bll.H1'
70 ********** */
71 IEmploy dal = new Hello();
72 ISurface dal3 = new Hello();
73 IEmploy dal2 = new Sorry();
74 public void SpeakHello()
75 {
76 dal.Speak( " 直接调用,hello类 " );
77 }
78 public void SpeakSorry()
79 {
80 dal2.Speak( " 直接调用,sorry的类 " );
81 }
82 public void SurfaceTest()
83 {
84 dal3.Surface();
85 }
86 public void ISurfaceSpeak()
87 {
88 dal3.Speak( " 使用ISurface接口的Speak " );
89 }
90 }
91
92 /// <summary>
93 /// 通过反射实例化
94 /// </summary>
95 public class bll2
96 {
97 IEmploy dal = (IEmploy)Create( " ConsoleTest.Hello " );
98 IEmploy dal2 = (IEmploy)Create( " ConsoleTest.Sorry " );
99
100 static string FullName = System.Reflection.Assembly.GetExecutingAssembly().FullName;
101 // 这里重新实现了接口的方法,可以给接口的方法进行扩展之类的处理
102 public void SpeakHello()
103 {
104 dal.Speak( " 反射调用,hello类 " );
105 }
106 public void SpeakSorry()
107 {
108 dal2.Speak( " 反射调用,sorry的类 " );
109 }
110
111 /// <summary>
112 /// 要返回的类,包含命名空间
113 /// </summary>
114 /// <param name="name"></param>
115 /// <returns></returns>
116 public static object Create( string name)
117 {
118 // 这由于是当前程序集使用,所以用这种方法获取,不知道还有没其他方法
119 Assembly assembly = Assembly.Load(FullName);
120 Type type = assembly.GetType(name);
121 object obj = Activator.CreateInstance(type);
122 return obj;
123 }
124 }
125
126 class Program
127 {
128 static void Main( string [] args)
129 {
130 bll b = new bll();
131 bll2 b2 = new bll2();
132 b.SpeakHello();
133 b2.SpeakHello();
134 b.SpeakSorry();
135 b2.SpeakSorry();
136 b.SurfaceTest();
137 b.ISurfaceSpeak();
138 System.Threading.Thread.Sleep( 100000 );
139 }
140 }
141 }
142
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Reflection;
6
7 namespace ConsoleTest
8 {
9 // 接口定义
10 interface IEmploy
11 {
12 // 继承该接口要实现的方法
13 void Speak( string s);
14 }
15
16 // 接口定义2
17 interface ISurface
18 {
19 void Speak( string s);
20 void Surface();
21 }
22
23
24 // Hello类实现接口,继承了2个接口
25 public class Hello : IEmploy, ISurface
26 {
27 // 实现接口1方法
28 public void Speak( string s) // 实现方法
29 {
30 Console.WriteLine( " Hello " + s);
31 }
32 // 实现接口2方法
33 public void Surface()
34 {
35 Console.WriteLine( " Test " );
36 }
37 // 实现方法2的Speak方法,为了避免编译冲突,必须使用显式实现
38 void ISurface.Speak( string s)
39 {
40 Console.WriteLine( " ISurface.Speak: " + s);
41 }
42 }
43
44 // Sorry类实现接口的方法
45 public class Sorry : IEmploy
46 {
47 public void Speak( string s)
48 {
49 Console.WriteLine( " Sorry " + s);
50 }
51 }
52
53 /// <summary>
54 /// 直接实例化使用
55 /// </summary>
56 public class bll
57 {
58 /* **********
59 1.类成员继承了IEmploy 只能通过 IEmploy 接口使用Speak方法
60 比如,不能如下的方法使用:
61 Hello h1 = new Hello();
62 h1.Speak("直接调用,hello类");
63
64 2.IEmploy,ISurface 接口的两个方法实现都是分离的,都不可以直接在类中使用
65
66 3.有个我不太理解,我以为下面是可以的,但是却不行
67 Hello H1=new Hello();
68 IEmploy test=(IEmploy)H1;
69 编译提示错误A field initializer cannot reference the non-static field, method, or property 'ConsoleTest.bll.H1'
70 ********** */
71 IEmploy dal = new Hello();
72 ISurface dal3 = new Hello();
73 IEmploy dal2 = new Sorry();
74 public void SpeakHello()
75 {
76 dal.Speak( " 直接调用,hello类 " );
77 }
78 public void SpeakSorry()
79 {
80 dal2.Speak( " 直接调用,sorry的类 " );
81 }
82 public void SurfaceTest()
83 {
84 dal3.Surface();
85 }
86 public void ISurfaceSpeak()
87 {
88 dal3.Speak( " 使用ISurface接口的Speak " );
89 }
90 }
91
92 /// <summary>
93 /// 通过反射实例化
94 /// </summary>
95 public class bll2
96 {
97 IEmploy dal = (IEmploy)Create( " ConsoleTest.Hello " );
98 IEmploy dal2 = (IEmploy)Create( " ConsoleTest.Sorry " );
99
100 static string FullName = System.Reflection.Assembly.GetExecutingAssembly().FullName;
101 // 这里重新实现了接口的方法,可以给接口的方法进行扩展之类的处理
102 public void SpeakHello()
103 {
104 dal.Speak( " 反射调用,hello类 " );
105 }
106 public void SpeakSorry()
107 {
108 dal2.Speak( " 反射调用,sorry的类 " );
109 }
110
111 /// <summary>
112 /// 要返回的类,包含命名空间
113 /// </summary>
114 /// <param name="name"></param>
115 /// <returns></returns>
116 public static object Create( string name)
117 {
118 // 这由于是当前程序集使用,所以用这种方法获取,不知道还有没其他方法
119 Assembly assembly = Assembly.Load(FullName);
120 Type type = assembly.GetType(name);
121 object obj = Activator.CreateInstance(type);
122 return obj;
123 }
124 }
125
126 class Program
127 {
128 static void Main( string [] args)
129 {
130 bll b = new bll();
131 bll2 b2 = new bll2();
132 b.SpeakHello();
133 b2.SpeakHello();
134 b.SpeakSorry();
135 b2.SpeakSorry();
136 b.SurfaceTest();
137 b.ISurfaceSpeak();
138 System.Threading.Thread.Sleep( 100000 );
139 }
140 }
141 }
142