I i = new A();
爲什麼我們可以使用接口I實例化類A的對象?我們不應該使用A obj = new A()?類實例化Java
interface I {
void f();
void g();
}
class A implements I {
public void f() { System.out.println("A: doing f()"); }
public void g() { System.out.println("A: doing g()"); }
}
class B implements I {
public void f() { System.out.println("B: doing f()"); }
public void g() { System.out.println("B: doing g()"); }
}
class C implements I {
// delegation
I i = new A();
public void f() { i.f(); }
public void g() { i.g(); }
// normal attributes
public void toA() { i = new A(); }
public void toB() { i = new B(); }
}
謝謝!
你能否澄清問題是什麼? – beny23 2012-04-13 12:32:29