`
liusu
  • 浏览: 170033 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

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

    博客分类:
  • Java
阅读更多
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 楼 liusu 2009-06-11  
偶尔还是会出现这样的状况得。。。
1 楼 mikeandmore 2009-06-11  
那interface的意义何在,不如直接就多继承好了。。。

相关推荐

Global site tag (gtag.js) - Google Analytics