比如
int a=instance1.function(i);
int b=instance2.function(f,t);
这2个方法调用并不涉及返回值

解决方案 »

  1.   

    fantasyCoder(牛仔+T恤) :
    这个我知道,我意思是头一段说接口可以继承相同签名的多个方法,为什么后面又说两个继承来的方法,只是在返回值上不一样,那么就会报错
      

  2.   

    签名?什么签名?看不懂。
    James gosling是java语言的创始人,他不会搞错的。请把英语原文贴出来。
      

  3.   

    Thinking in java 中有說明
    也就是java無法僅根據返回值類型區分方法
     fantasyCoder(牛仔+T恤)說的很清楚了--
    这个我知道,我意思是头一段说接口可以继承相同签名的多个方法,为什么后面又说两个继承来的方法,只是在返回值上不一样,那么就会报错
    --這里指的應該是多態
      

  4.   

    我查了一下,英文版应该是112页的最后一段:
      If an interface inherits more than one method with the same 
    signature, or a class implements different interfaces containing
    a method with the same signature,there is only one such method-
    whose implementation is ultimately defined by the class implementing
    the interfaces.There is no question of ambiguity in this case.  这段话的意思是这样的,假设有两个接口A和B,各提供一个方法,接口C
    继承A和B:
      interface A{
          void func();
      }
      interface B{
          void func();
      }
      interface C extends A,B{
         //......
      }
      很明显,C中不可能有两个void func()
      同样的,假设有一个类D现A,B接口:
      class D implements A,B{
          void func();
      }
      D中也只能有一个func()  Java不能只根据返回值区别不同方法,如果是下面这样写
    就会报错:
      interface A{
          void func();
      }
      interface B{
          int func();
      }
      interface C extends A,B{
          void func();
          int func(); 
      }
      class D implements C{
          void func();
          int func();
      }(*)翻译有问题
      “如果某个声明的方法和继承方法,或者两个继承来的方法,只是在
    返回值不一样,那么就会报错。”
          |
        的类型上
    p113:
       If a declared method differs only in return type from an 
    inherited method, or if two inherited methods differ only in 
    return type, then an error occurs.
      

  5.   

    OK
    我想我知道是怎么回事了
    谢了!
    但又产生两个新问题:
    interface A{
          void func();
      }
      interface B{
          void func();
      }
    class D implements A,B{
          void func();
      }
    1.那么D中的实现的void func()是接口A的还是接口B的?
    2.这样一来D是不是应该被声明为abstract的,因为A和B中肯定有一个接口的方法没有被全部实现。
      

  6.   

    顺便再说一句,不是我不想帖英文原文,而是现在我手头没有。所以哪位知道哪里有英文的The Java Proramming Language请告诉我。
      

  7.   

    interface A{
          void func();
      }
      interface B{
          void func();
      }
    class D implements A,B{
          void func();
      }//这样编译肯定会出错的!
      

  8.   

    运行一下吧,绝对OK。这样写的话D实际上只实现A,
    B被忽略了。interface A{
          void func();
    }
    interface B{
          void func();
    }
    public class D implements A,B{
          public void func(){
              System.out.println("No problem!");
          }
          public static void main(String[] args){
     D d=new D();
              d.func();
          }
    }