想彻底明白, 看看think in java.
没错,是java的书,很多高手把它说明得很详细,再用C#语法写就可以了。
甚至,用java写然后用 javatoc#的软件一转就可以了。
没错,是java的书,很多高手把它说明得很详细,再用C#语法写就可以了。
甚至,用java写然后用 javatoc#的软件一转就可以了。
解决方案 »
- 如何实现窗体像qq一样可以拖动到上方或者右侧隐藏
- C#中如何将URL中的一副图片显示提取并显示到 PictureBox控件中?
- 强制分页打印
- 高人快来!谁能告诉我这个WEB应用程序打包的问题啊!
- 求一个 DataTable 与 HashTable互转类。
- 请问如何写钩子程序能够钩住其他软件的TEXTBOX控件,并能获得系统传给他的值.
- 怎样在网页间传递datatable类型的数据?
- 毕业设计遇到了大难题啊(DATAGRID,导出,EXCEL)(非WEB的)大家请帮忙啊。。。
- 这个小问题我还是第一次遇到,平时不怎么注意.
- 请问,在vb.net中的模块(Module)在c#中怎么用?
- 软件界面编程的小疑问?
- 关于ListBox空间绑定、选择问题
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");
}
}
你无法预知会有什么样的对象要被创建,而且你也很难预知被创建的对象会如何运作,但是你却知道对象会有那些动作,那么你就可以使用接口了。例如你知道你的应用中会有一个对象,会执行一个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是无法做到的。
你说的问题建议看看设计模式(Design patterns)中的Abstract Factory,Facotory Method等创建型模式.
----------------------------------------------------------------------
对于你现在的这种做法,当然是没有任何意义的,因为具体用到的那个类,你都是硬编码在Code里面的。但是如果你把这些可变元放到配置文件中,那么意义就很明显了,用户只要改变配置文件,就能获得不同的体验。比如插件,就是公开了一个公共的接口,你可以开发,我也可以开发,只要都最终实现了这个接口就行。
一个很简单的便例子,你写一个数据访问层,里面的所有操作的都是基本于
IDBConnection,IDBCommand,这样你的访问层代码除了能处理SQLCLIENT,还可以处理OleDb,Oracle或所有从其接口派生出来的类。
这样可以提高代码的重用性,还减低了代码之间的偶合度。
xiongchen(二氧化鬼) 说的好象是类工厂的方法吧真是很好的建议谢谢,在下愚钝再问几句
比如说我有一个单据处理系统,有很多种单据(入库单,出库单等等)将来可能还会增加新的单据
但是这些单据都会有单号,日期属性和保存,查找方法,我想定义一个单据接口包含这些属性和方法具体的单据类继承这个单据接口,但是我的界面程序都是不同的我要在每一个单据处理界面处理相应的单据即调用相应的类,这个接口有什么用呢,如果要面向接口该怎么做呢?