ITeye博客

多个interface有完全相同的签名方法得情况,C#比Java得处理似乎更合理一点

liusu · 2009-06-11 11:15 · Java · 非技术 · Markdown

Interface A:


package snippet;

public interface IAPaint {
	public void paint();
}

Interface B:


package snippet;

public interface IBPaint {
	public void paint();
}


这样实现就可以了:


package snippet;

public class ABImpl implements IAPaint, IBPaint {

	public void paint() {
		System.out.println("Paint");
	}

	public static void main(String[] args) {
		IAPaint implA = new ABImpl();
		implA.paint();

		IBPaint implB = new ABImpl();
		implB.paint();
	}
}


实现了以后,A和B接口实现共享同个方法体。

呵呵,c#可以这样:


public class SampleClass : IControl, ISurface
{
    void IControl.Paint()
    {
        System.Console.WriteLine("IControl.Paint");
    }
    void ISurface.Paint()
    {
        System.Console.WriteLine("ISurface.Paint");
    }
}


评论(2)

  1. #2 楼 · liusu · 2009-06-11 12:50

    偶尔还是会出现这样的状况得。。。

  2. #1 楼 · mikeandmore · 2009-06-11 12:21

    那interface的意义何在,不如直接就多继承好了。。。

评论功能维护中,当前仅展示历史评论。