以内部类方法解决接口重名问题

  在JAVA中,当我们对一个类implements两个interface时,两个interface中含有同名函数会导致编译报错。如果我们一定要同时调用这两个函数,并且不能对原接口进行改动时,我们如何解决呢?

  答案是内部类(inner class)。代码如下:

 1 interface I1{
 2     public int f();
 3 }
 4 
 5 interface I2{
 6     public void f();
 7 }
 8 
 9 
10 class J  implements I1{
11     public int f() {
12         return 0;
13     }
14     class content implements I2{
15         public  void f() {
16     }
17  }
18 }
View Code

  至此,我们就解决了接口内函数重名问题。

posted @ 2017-11-01 08:49  望山海  阅读(152)  评论(0编辑  收藏  举报