java 兩個 interface 中相同名稱的method

本帖最後由 shadow1638 於 2010-4-30 01:35 編輯

請問各位大大,如果兩個 interface 中有相同名稱的method,例如下面的例子中,class A 會overriding 掉 interface I1 的 f1 method,還是 I2 的 f1 method? 為什麼?
感謝
  1. interface I1 {
  2.    void f1();
  3. }

  4. interface I2 {
  5.    void f1();
  6. }

  7. class A implements I1,I2 {
  8.    public void f1() {
  9.       System.out.println("hai");
  10.    }
  11. }

  12. public class Intrs {
  13.    public static void main(String[] S) {
  14.       A ob=new A();
  15.       ob.f1();
  16.    }
  17. }
複製代碼

finally only one f1 in A
not belongs to I1 nor I2

TOP

finally only one f1 in A
not belongs to I1 nor I2
DarkHero 發表於 2010-4-30 01:33



    thanks for the answers, but why not there are two f1 in A, because it have implements two interface?

TOP

另外想問下,super能否應用在interface?

TOP

thanks for the answers, but why not there are two f1 in A, because it have implements two int ...
shadow1638 發表於 2010-4-30 01:37

from the concept of c++
it will not know which one you need to run as both method are the same with its method identifier

TOP

from the concept of c++
it will not know which one you need to run as both method are the same with ...
DarkHero 發表於 2010-4-30 02:46



小弟是java新手,沒有學c++,睇書見到java的sub class不能多重繼承super class,但睇到interface又話interface可以咁做。不過implements兩個有相同名的interface時,jvm怎知要overriding那個method...如果照大大的講法,jvm只知道要實作f1() method,而不理會f1() method從那裡來?

TOP

http://www.linuxtopia.org/online ... java/TIJ310_001.htm


Using the same method names in different interfaces that are intended to be combined generally causes confusion in the readability of the code, as well. Strive to avoid it.

TOP

Using the same method names in different interfaces that are intended to be combined generally ca ...
travel 發表於 2010-4-30 11:20


這句明白了,thanks

TOP