Cho chương trình sau.Cho biết kết quả sau khi thực thi chương trình trên?
public class TBT {
public void method(Object o) {
System.out.println("Object Verion");
}
public void method(String s) {
System.out.println("String Version");
}
public static void main(String args[]) {
TBT question = new TBT();
question.method(null);
}
}
public class TBT {
public void method(Object o) {
System.out.println("Object Verion");
}
public void method(String s) {
System.out.println("String Version");
}
public static void main(String args[]) {
TBT question = new TBT();
question.method(null);
}
}
public void method(Object o) {
System.out.println("Object Verion");
}
public void method(String s) {
System.out.println("String Version");
}
public static void main(String args[]) {
TBT question = new TBT();
question.method(null);
}
}
Trả lời:
Đáp án đúng: B
Trong đoạn code trên, khi gọi `question.method(null)`, trình biên dịch sẽ ưu tiên chọn overload method cụ thể hơn. Ở đây, `String` là một lớp con của `Object`, do đó `method(String s)` cụ thể hơn `method(Object o)`. Vì vậy, trình biên dịch sẽ chọn `method(String s)` và in ra "String Version".