的差I有如下的java代碼:上溯造型對象 - 靜態和非靜電型
class A {
int someMethod() { return 1; }
int someMethod (A a) { return 2; }
int someMethod (B b) { return 3; }
int someMethod (C c) { return 4; }
static A anotherMethod (Object obj) { return (A) obj; }
}
class B extends A {
int someMethod() { return 6; }
int someMethod (A a) { return 7; }
int someMethod (B b) { return 8; }
int someMethod (C c) { return 9; }
static A anotherMethod (Object obj) { return (B) obj; }
}
class C extends A {
int someMethod() { return 11; }
int someMethod (A a) { return 12; }
int someMethod (B b) { return 13; }
int someMethod (C c) { return 14; }
static C anotherMethod (Object obj) { return (C) obj; }
}
public static void main (String [] args){
A a = new A(); B b = new B(); C c = new C();
System .out. println (A. anotherMethod (b). someMethod (b));
}
如所預期的輸出是8
確定現在我刪除的someMethod(B B)中A級:
class A {
int someMethod() { return 1; }
int someMethod (A a) { return 2; }
int someMethod (C c) { return 4; }
static A anotherMethod (Object obj) { return (A) obj; }
}
我討論與我的朋友的輸出,但沒有人能夠解釋到底爲什麼我們得到了一個7作爲輸出現在????!?!
C類在哪裏? – Touchstone 2015-04-02 08:39:34
您是否知道您無法重寫'static'方法?你有什麼是語法上有效的,但'靜態'方法*不會互相覆蓋。 – JonK 2015-04-02 08:45:00