interface I {
void f();
}
// -----------------------------------------------------------
class C {
public void f() {
System.out.println ("C.f() is called.");
}
}
// -----------------------------------------------------------
类 A 和 类C 、接口I之间的关系如下:
class A extends C implements I {
//请问:这儿的f()我觉得应该是实现了I才对,而不是override了C中的f(),请问怎样证明?
public void f() {
System.out.println ("TestOverride.f() is called.");
}
}

解决方案 »

  1.   

    A a = new A();
            a.f();既实现了I,也覆盖了C
      

  2.   

    如果父类中的方法与接口中方法声明一致;则在子类中不必实现接口的方法;如果子类中实现了接口的方法,实际上不是实现,而是覆盖;
    因为extends起作用比implements早,当implements时,父类中的方法可以认为已经实现了接口,
    因此这里是overriden而不是implemnts
      

  3.   

    测试
    interface I 
    {
    void f();
    }class C {
    public void f() 
    {
    System.out.println ("C.f() is called.");
    }
    }public class A extends C implements I 
    {
    //请问:这儿的f()我觉得应该是实现了I才对,而不是override了C中的f(),请问怎样证明?
    public void f() 
    {
    System.out.println ("TestOverride.f() is called.");
    }

    public static void main(String[] args)
    {
    I  iInstance = new A();
    C  cInstance = new A();

    iInstance.f();
    cInstance.f();
    }
    }
    结果如下:
    F:\>javac A.javaF:\>java A
    TestOverride.f() is called.
    TestOverride.f() is called.F:\>很明显它既实现了I,也覆盖了C,如果不是覆盖了C的话,第二语句应该输出C.f() is called.才是!
      

  4.   

    crazycy(代言人)说的如果父类中的方法与接口中方法声明一致,则在子类中不必实现接口的方法,也就是说子类会把从父类继承过来的同名方法当作实现了接口,实验如下:
    interface I
    {
        void f();
    }
    class C
    {
        public void f()
        {
            System.out.println ("C.f() is called.");
        }
    }
    class A extends C implements I 
    {
    /*    public void f() 
        {
            System.out.println ("TestOverride.f() is called.");
        }*/
    }
    public class OverAndImpl {
        
        /** Creates a new instance of OverAndImpl */
        public OverAndImpl() {
        }
        
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            // TODO code application logic here
            new A().f();
        }
        
    }
      

  5.   

    interface中的f只不过是个placeholder,只要你的class里面有f方法,不论是继承的还是自己实现的,或者override,都是implement了interface中的f
      

  6.   

    Java编程思想第二版252-253页有说明