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.");
}
}
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.");
}
}
a.f();既实现了I,也覆盖了C
因为extends起作用比implements早,当implements时,父类中的方法可以认为已经实现了接口,
因此这里是overriden而不是implemnts
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.才是!
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();
}
}