#region Using directivesusing System;
using System.Collections.Generic;
using System.Text;#endregionnamespace ExtendAndCombineInterface
{
   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
   {      // hold the data for IStorable's Status property
      private int status = 0;      // 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" );      }
   }   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" );
      }
   }
}Output:
Creating document with: Test Document
Implementing the Read Method for IStorable
Implementing Compress
Implementing LogSavedBytes
Implementing Compress
Implementing LogOriginalSize
Implementing LogSavedBytes
Implementing Compress
Implementing the Read Method for IStorable
Implementing Encrypt
在其中Main()里面有一段代码:
IStorable isDoc = doc as IStorable;
         if ( isDoc != null )
         {
            isDoc.Read( );
         }
         else
            Console.WriteLine( "IStorable not supported" );接口不是不能被实体化的吗? doc是Document的对象.那么doc as IStorable是什么意思呢?
isDoc到底是什么呢?
判断isDoc != null 到底是判断什么呢?我看的是英文教材,里面解释是:
The Tester program creates a new Document object and then uses it as an instance of the various interfaces. You are free to cast:ICompressible icDoc = doc as ICompressible;这里的object和instance有什么区别啊?多谢拉,我刚开始学C#

解决方案 »

  1.   

    对了还有就是isDoc和doc之间是什么关系啊?
      

  2.   

    这里使用as是不是就是在两个引用类型间转换呢?doc是Document类型的,这里ICompressible icDoc = doc as ICompressible;就是把doc转换成ICompressible接口类型的对象,如果不能转换就返回NULL,那么icDoc和doc之间是什么关系呢?他们是各指向同一个内存地址是引用关系呢,还是icDoc是一个单独的对象呢?
    这是我刚上网查AS的用法后的想法,麻烦大家给点意见吧
      

  3.   

    相当于:
    IStorable isDoc = doc is IStorable ? (IStorable)doc : null;
    C#中是由一种办法进行创建实例,那就是“new”,这不是你的实例化的概念,而是一种类型转换或者说是用基类(也就是接口)的指针去指向一个子类实例对象。
      

  4.   

    显然如果 doc.GetType 如果不是 IStorable,则 isDoc 将被设置为空引用,也就是说“isDoc != null”实际上是判断了doc.GetType 是不是实现了接口 IStorable。
      

  5.   

    你可以将对象和实例等同,我们经常说“对象实例”。英文注释的意思是将 Document 类型的一个对象实例作为Document所实现接口的实例。从指针角度和c++的角度就是用基类的指针指向一个子类的对象实例,只是在指向前首先判断子类是否实现了某个基类,如果没有实现则将这个基类指针设置为空引用。