想彻底明白,  看看think in java.
没错,是java的书,很多高手把它说明得很详细,再用C#语法写就可以了。
甚至,用java写然后用  javatoc#的软件一转就可以了。

解决方案 »

  1.   

    接口主要是由类来使用,从某种意义上说就是一个抽象类,不能直接实例化,但可以申明变量; 此外接口成员必须独在使用接口的类上执行(因为他们没有代码体);接口也不能包含字段、构造函数、析构函数、静态成员或者说常量。给个完整的扩展和结合接口的例子:
    C#中没有多重继承,但可以通过接口实现类似的功能
    using System;interface IStorable
    {
       void Read();
       void Write(object obj);
       int Status { get; set; }}// here's the new interface
    interface ICompressible
    {
       void Compress();
       void Decompress();
    }// Extend the interface
    interface ILoggedCompressible : ICompressible
    {
       void LogSavedBytes();
    }// Combine Interfaces
    interface IStorableCompressible : IStorable, ILoggedCompressible
    {
       void LogOriginalSize();
    }// yet another interface
    interface IEncryptable
    {
       void Encrypt();
       void Decrypt();
    }public class Document : IStorableCompressible, IEncryptable
    {
       // the document constructor
       public Document(string s) 
       {
          Console.WriteLine("Creating document with: {0}", s);
            
       }
        
       // implement IStorable
       public void Read()
       {
          Console.WriteLine(
             "Implementing the Read Method for IStorable");        
       }   public void Write(object o)
       {
          Console.WriteLine(
             "Implementing the Write Method for IStorable");  
       }   public int Status
       {
          get
          {
             return status;
          }      set
          {
             status = value;
          }
       }
        
       // implement ICompressible
       public void Compress() 
       { 
          Console.WriteLine("Implementing Compress"); 
       }
        
       public void Decompress() 
       { 
          Console.WriteLine("Implementing Decompress"); 
       }
        
       // implement ILoggedCompressible
       public void LogSavedBytes()
       {
          Console.WriteLine("Implementing LogSavedBytes");
       }   
        
       // implement IStorableCompressible 
       public void LogOriginalSize()
       {
          Console.WriteLine("Implementing LogOriginalSize");
       }   // implement IEncryptable
       public void Encrypt()
       {
          Console.WriteLine("Implementing Encrypt");
            
       }   public void Decrypt()
       {
          Console.WriteLine("Implementing Decrypt");
            
       }   // hold the data for IStorable's Status property
       private int status = 0;
    }public class Tester
    {
     
       static void Main()
       {
          // create a document object
          Document doc = new Document("Test Document");      // cast the document to the various interfaces
          IStorable isDoc = doc as IStorable;
          if (isDoc != null)
          {
             isDoc.Read();
          }
          else
             Console.WriteLine("IStorable not supported");
            
          ICompressible icDoc = doc as ICompressible;
          if (icDoc != null)
          {
             icDoc.Compress();
          }
          else
             Console.WriteLine("Compressible not supported");      ILoggedCompressible ilcDoc = doc as ILoggedCompressible;
          if (ilcDoc != null)
          {
             ilcDoc.LogSavedBytes();
             ilcDoc.Compress();
             // ilcDoc.Read();
          }
          else
             Console.WriteLine("LoggedCompressible not supported");      IStorableCompressible isc = doc as IStorableCompressible;
          if (isc != null)
          {
             isc.LogOriginalSize();  // IStorableCompressible
             isc.LogSavedBytes();    // ILoggedCompressible
             isc.Compress();         // ICompressible
             isc.Read();             // IStorable      }
          else
          {
             Console.WriteLine("StorableCompressible not supported");
          }      IEncryptable ie = doc as IEncryptable;
          if (ie != null)
          {
             ie.Encrypt();
          }
          else
             Console.WriteLine("Encryptable not supported");
       }
    }
      

  2.   

    我们可以设想一下以下的情况:
    你无法预知会有什么样的对象要被创建,而且你也很难预知被创建的对象会如何运作,但是你却知道对象会有那些动作,那么你就可以使用接口了。例如你知道你的应用中会有一个对象,会执行一个Operation的动作,那么可以这样做:
    public interface IFoo
    {
       void Operation();
    }
    而后,所有要执行Operation操作的对象都继承IFoo接口。
    例如有一个对象FooObjA
    public class FooObjA:IFoo
    {
       public void Operation()
       {
           //user code;
       }
    }FooObjA实现了Operation的操作,而不用关心其它。
    其实这样还是无法了解接口的实际作用,因为这样可以使用Abstract Class来替换接口。那么我们再设想一下,有一个Observer的对象,它负责记录下已有的对象,而不管对象是谁创建,也不管对象做什么用,但是这些对象都是继承IFoo的。
    而在系统的另一边,有一些对象需要从Observer获取这些对象中符合它们要求的对象,然后执行Operation操作,即:
    IFoo foo=Observer("some requirement");
    foo.Operation();
    这样,调用者从来都不需要知道Observer另一边的情况,只需要知道有符合它们要求的的对象存在,就足够了。而这一点,Abstract Class是无法做到的。
      

  3.   

    http://search.csdn.net/Expert/topic/1144/1144424.xml?temp=9.444827E-02还有这一贴,都是些高手在讨论,可以看看,我一直收藏着!
      

  4.   

    接口只是让你知道一个约定,如有哪些函数,函数有哪些参数,返回什么.
    你说的问题建议看看设计模式(Design patterns)中的Abstract Factory,Facotory Method等创建型模式.
      

  5.   

    这样在组件2中也必须知道接口的具体实现,当我在组件1中将class1换成class3,组件2中也要相应的改成 组件1.I1 i1=new 组件1.class3();这和不用接口有什么区别,怎么才算是使用接口隔离具体实现。
    ----------------------------------------------------------------------
    对于你现在的这种做法,当然是没有任何意义的,因为具体用到的那个类,你都是硬编码在Code里面的。但是如果你把这些可变元放到配置文件中,那么意义就很明显了,用户只要改变配置文件,就能获得不同的体验。比如插件,就是公开了一个公共的接口,你可以开发,我也可以开发,只要都最终实现了这个接口就行。
      

  6.   

    接口除是是一个约定外,本身也是个抽象体。
    一个很简单的便例子,你写一个数据访问层,里面的所有操作的都是基本于
    IDBConnection,IDBCommand,这样你的访问层代码除了能处理SQLCLIENT,还可以处理OleDb,Oracle或所有从其接口派生出来的类。
    这样可以提高代码的重用性,还减低了代码之间的偶合度。
      

  7.   

    谢谢大家的发言尤其是xiongchen(二氧化鬼)兄,快过年了祝大家新春快乐、万事如意
    xiongchen(二氧化鬼) 说的好象是类工厂的方法吧真是很好的建议谢谢,在下愚钝再问几句
    比如说我有一个单据处理系统,有很多种单据(入库单,出库单等等)将来可能还会增加新的单据
    但是这些单据都会有单号,日期属性和保存,查找方法,我想定义一个单据接口包含这些属性和方法具体的单据类继承这个单据接口,但是我的界面程序都是不同的我要在每一个单据处理界面处理相应的单据即调用相应的类,这个接口有什么用呢,如果要面向接口该怎么做呢?
      

  8.   

    ssDOn的例子很完整,让我受益匪浅.Up