帮助大家学习对接口和反射理解,另外帮我解惑下其中的一个问题

代码
  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 

 

转载于:https://www.cnblogs.com/wyxy2005/archive/2010/06/03/1750941.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值