我有2个类,,类里面使用呢委托....
公共方法和使用的委托都是一样的(不想使用静态的委托) ....
就是执行的内容不一样...我现在给这两个类..定义呢一个接口......可是 公共方法都没问题... 可是类里面的委托怎么办...

解决方案 »

  1.   

    delegate is NOT a method, it is a type (a class, to be exact).
    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);
            }
        }
      

  2.   

    楼上给的列子是委托事件
    如果是直接委托方法该怎么写呢    public delegate void logText(string text);
        public interface IMy
        {
        }    public class MyOne : IMy
        {
            logText set_logText;
        }
      

  3.   


    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.
    }