我有2个类,,类里面使用呢委托....
公共方法和使用的委托都是一样的(不想使用静态的委托) ....
就是执行的内容不一样...我现在给这两个类..定义呢一个接口......可是 公共方法都没问题... 可是类里面的委托怎么办...
解决方案 »
- [求助] 利用C#如何使form全屏时两个画面快速切换?(是快速!)
- C#画字符串的方法
- 请教个winform的问题
- 这种类关系图是用什么软件画出来
- 更改RichTextBox中选中文本字体的问题
- 如何将四个字节转成一个IEEE浮点数(C#)?
- Do you think Japan should become a permanent member of the United Nations Security Council ?
- 请问关于N层与模块功能上的问题
- .net下的持久层框架、ORM都有哪些?大家帮我写下名字和支持网站还有你对它们的评价好吗?
- 手写识别,合作
- 一段关于ProgressBar的C#程序
- 高手来看这段网络编程代码,有何问题
You don't put a delegate into an interface:
delegate void MyMethod(string s); //<---put it outside the interface
interface IMy
{
void DoWork(MyMethod m);
}
If you are talking about 'Event', then public interface IMy
{
event EventHandler Greet; //<---declared an interface event
void SayHello();
} public class MyOne : IMy
{
public event EventHandler Greet;
public void SayHello()
{
Console.WriteLine("MyOne says hello");
if (Greet != null) Greet(this, EventArgs.Empty);
}
}
public class MyTwo : IMy
{
public event EventHandler Greet;
public void SayHello()
{
Console.WriteLine("MyTwo says hello");
if (Greet != null) Greet(this, EventArgs.Empty);
}
}
如果是直接委托方法该怎么写呢 public delegate void logText(string text);
public interface IMy
{
} public class MyOne : IMy
{
logText set_logText;
}
logText set_logText;保存的是LogText的一个实例,就像
string logName;保存一个string实例一样。很明显,string logName在C#中是没有interface原形的,logText也一样:
public class MyOne : IMy
{
string logName;
}但是,logName的Setter和Getter却可以有interface原形:
interface IMy
{
string LogName {get;set;}
}同理,delegate set_logText的Getter和Setter也可以有interface原形。
再深入一步,其实interface Event声明的,实际上是Event的Adder和Remover:
interface IMy
{
string LogName { get; set;}
logText LogText { get; set;}
event logText LogEvent; //<---
}
class My : IMy
{
public string LogName { get { return null; } set { } }
public logText LogText { get { return null; } set { } }
public event logText LogEvent { add { } remove { } } //<---implement the Adder and Remover actually.
}