Ich weiß, dass das Überladen statische Bindung verwendet und das Überschreiben dynamische Bindung verwendet. Aber was ist, wenn sie gemischt sind? Nach this tutorial, um Methodenaufrufe zu lösen, verwendet statische Bindung Typ Informationen, während dynamische Bindung tatsächliche Objektinformationen verwendet.Fall: statische Bindung? Dynamische Bindung?
Gibt es im folgenden Beispiel eine statische Bindung, um zu ermitteln, welche sort()
-Methode aufgerufen werden soll?
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: Die Ausgabe ist: Child#sort(Collection c) is invoked
Was ist der Ausgang? –