问题
假设我试图将手机模型化为普通手机和PDA的组合.这是一种多重继承方案(手机是手机,它是 PDA).由于C#不支持多重继承,因此这几乎需要某种组合.另外,让我们说我还有其他理由支持作曲.
我一直想知道:是否有任何工具可以自动生成所有不可避免的传递代码?
让我用一些实际的代码充实我的例子:
接口:
public interface IPhone
{
public void MakeCall(int phoneNumber);
public void AnswerCall();
public void HangUp();
}
public interface IPda
{
public void SendEmail(string[] recipientList, string subject, string message);
public int LookUpContactPhoneNumber(string contactName);
public void SyncWithComputer();
}
Run Code Online (Sandbox Code Playgroud)
实现:
public class Phone : IPhone
{
public void MakeCall(int phoneNumber) { // implementation }
public void AnswerCall() { // implementation }
public void HangUp() { // implementation }
}
public class Pda : IPda
{ …Run Code Online (Sandbox Code Playgroud) 我有一个静态类,它包含winspool中的一些本机方法:
public static class WinSpool
{
[DllImport("winspool.drv")]
public static extern int OpenPrinter(string pPrinterName, out IntPtr phPrinter, IntPtr pDefault);
...
//some more methods here
}
Run Code Online (Sandbox Code Playgroud)
我想模仿它们进行单元测试,但是找不到这种模式.(每个人都避免使用静态类吗?)
我想采用一个.NET类(为了讨论而让我们说FileInfo)并让它实现我的接口.例如:
public interface IDeletable
{
void Delete();
}
Run Code Online (Sandbox Code Playgroud)
请注意,FileInfo DOES具有Delete()方法,因此该接口可能有意义.
这样我就可以得到这样的代码:
FileInfo fileinfo = ...;
DeletObject(fileInfo);
public void DeleteObject(IDeletable deletable)
{
deletable.Delete();
}
Run Code Online (Sandbox Code Playgroud)
是否可以使现有的类匹配这样的接口?