请教关于接口实现的问题
#region Using directives
using System;
using System.Collections.Generic;
using System.Text;
#endregion
namespace 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#
[解决办法]
相当于:
IStorable isDoc = doc is IStorable ? (IStorable)doc : null;
C#中是由一种办法进行创建实例,那就是“new”,这不是你的实例化的概念,而是一种类型转换或者说是用基类(也就是接口)的指针去指向一个子类实例对象。
------解决方案--------------------
你可以将对象和实例等同,我们经常说“对象实例”。英文注释的意思是将 Document 类型的一个对象实例作为Document所实现接口的实例。从指针角度和c++的角度就是用基类的指针指向一个子类的对象实例,只是在指向前首先判断子类是否实现了某个基类,如果没有实现则将这个基类指针设置为空引用。