两个接口中有同样方法,又要实现这两个接口怎么办?下面有一段代码,怎样正确执行?
interface A {
  public void xx() {
  }
}interface B {
  public int xx() {
    return 0;
  }
}public class C implements A,B {
  public C() {
  }
  
  //这里如何写接口A,B方法的实现  public static void main(String[] args) {
    C cc = new C();
  }
}

解决方案 »

  1.   

    在实现中明式写出接口的名字,
    interface A { 
      public void xx(); 
    } interface B { 
      public int xx();
    } public class C implements A,B { 
      public C() { 
      } 
      public void A.xx(){
        //
      }
      public int B.xx(){
        //
      }
         public static void main(String[] args) { 
        C cc = new C(); 
      } 
    }
    另外如果是面试题的话直接走人,
    interface A { 
      public void xx() { 
      } 
    } interface B { 
      public int xx() { 
        return 0; 
      } 
    } 没有这样写接口的。
      

  2.   

    我觉得曲线实现:
    1、用一个类来实现interface A中的xx()方法,第二个类实现interface B中的xx()方法,同时在第二个类中继承第一个类。
    2、用一个类来实现interface A中的xx()方法,同时在这个类中声明一个匿名内部类,这个匿名内部类实现interface B中的xx()方法。
      

  3.   

    呵呵,不好意思,咱的接口方法写错了,不过要表达的意思就是这个,我看了大家的帖子,自己也试验过了。interface A {
    public void xx();
    }interface B {
    public int xx();
    }public class C implements A,B {
    public C() {
    System.out.println("C is ok!");
    }
    public void A.xx() {
    System.out.println("ok!");
    }
    public int B.xx() {
    System.out.println("B is ok");
    return 0;
    }
    public static void main(String[] args) {
    C cc = new C();
    cc.A.xx();
    }
    }
    //要是这样的话,实在是没办法实现的,以上代码编译通不过,主要是方法不能B.xx()或A.xx(),就算可
    //以,你如何调用cc的xx,总不能cc.A.xx(),这样肯定不行。
    //eclipse 提示解决办法是重命名接口方法还有一种代码是:interface A {
    public void xx();
    }interface B {
    public void xx();
    }public class C implements A,B {
    public C() {
    System.out.println("C is ok!");
    }
    public void xx() {
    System.out.println("ok!");
    }

    public static void main(String[] args) {
    C cc = new C();
    cc.xx();
    }
    }
    //这样的话可以编译运行,class C中写一个xx()方法就行了,没啥问题的!这就是我的总结了,不知道那为大侠有更好的想法和结论,发表下!
      

  4.   


    不明白为什么要这么写?不会有这种情况。我觉得:
    1.把xx这个抽象方法放在一个接口中就行了,没必要放两个接口里;
    2.如果业务需要必须写两个xx方法(一个void、一个int)的话,那只有改其中一个方法的方法名了。
      

  5.   

    在C#当可以用命名空间来区别实现的是哪一个接口,但JAVA还没遇到这种情况
      

  6.   


    interface A { 
      public void xx(); 
    } interface B { 
      public int xx();
    } public class C implements A,B { 
      public C() { 
      } 
      public void A.xx(){
        //
      }
      public int B.xx(){
        //
      }
         public static void main(String[] args) { 
        C cc = new C(); 
      } 
    }
    12楼那是C#的写法java没这种的
      

  7.   

    接口不会这样写的啊,如果遇到这种情况,可以用内部类来写interface  A {
    public void xx();
    }interface B {
    public int xx();
    }public class C implements A {        //类C实现A接口
        private int a = 0;
        private int b = 1;
        
        public void xx() {               //A接口中的xx()方法
         System.out.println(a);
        }
        
        public int ShowB() {             
         CC cc1 = new CC();
         return cc1.xx();
        }
        
        class CC implements B {          //内部类CC实现B接口
         public int xx() {            //B接口的xx()方法
         return b;
         }
        }
        
        public static void main(String[] args) {
         C c1 = new C();     c1.xx();                        //调用实现接口A中的xx()方法,输出a值
        
         System.out.println(c1.ShowB());//调用ShowB方法来调用在内部类中实现接口B的方法xx(),输出b值
        
        }
    }
      

  8.   

    public void A.xx(){//  }
    public int B.xx(){}jdk 5 6 都没有这样的语法
    7不知道有没有
      

  9.   

    public int A.xx(){}
    C#中可以这么去实现  但前面不能加public 
    java中不行的 
      

  10.   


    package com.llh.Test;interface A { 
    public void xx();
    } interface B { 
    public int xx();
    }
    class C implements A {
    class BB implements B {
    public int xx() {
    System.out.println("B.xx()");
    return 0;
    }
    }

    public B getB() {
    return new BB();
    }

    public void xx() {
    System.out.println("A.xx()");
    }
    }
    public class Test2 {
    public static void main(String[] args) {
    C c = new C();
    c.xx();
    //直接创建内部类对象
    C.BB b = c.new BB();
    b.xx();
    //通过方法获得内部类对象引用
    c.getB().xx();
    }
    }
      

  11.   

    最最简单不过的方法,那就是改个方法名就OK了,写程序用不着死钻牛角尖。不过,现实中很难遇到,但代码量大时,一个团队中可能出现这样的情况,同理,如上,改名。。IDE帮忙很方便。