我知道,重載使用靜態綁定和重寫使用動態綁定。 但是,如果他們混合? 根據this tutorial,爲了解析方法調用,靜態綁定使用類型信息,而動態綁定使用實際的對象信息。大小寫:靜態綁定?動態綁定?
那麼,在下面的例子中是否發生了靜態綁定,以確定調用哪個sort()
方法?
public class TestStaticAndDynamicBinding {
@SuppressWarnings("rawtypes")
public static void main(String[] args) {
Parent p = new Child();
Collection c = new HashSet();
p.sort(c);
}
}
。
public class Parent {
public void sort(Collection c) {
System.out.println("Parent#sort(Collection c) is invoked");
}
public void sort(HashSet c) {
System.out.println("Parent#sort(HashSet c) is invoked");
}
}
。
public class Child extends Parent {
public void sort(Collection c) {
System.out.println("Child#sort(Collection c) is invoked");
}
public void sort(HashSet c) {
System.out.println("Child#sort(HashSet c) is invoked");
}
}
PS: 的輸出是:在編譯時期間 Child#sort(Collection c) is invoked
什麼是輸出? –