이 코드를 실행 :VTBL은 Java에서 어떻게 구현됩니까?
class A {
public int x;
public A() {
function();
}
public void function() {
this.x = 20;
}
public void printhey() { System.out.println("Hey"); }
}
class B extends A {
public B() {
super();
printhey();
}
public void function() {
this.x = 50;
}
}
public class tmp {
public static void main(String[] args) {
System.out.println((new B()).x);
}
}
인쇄 아웃 :
Hey
50
는 누가 VTBL을 설정? A
의 생성자에서 function
은 이미 B로 설정되어 있습니다. 그러나 B
의 생성자에서 printhey
은 A로 설정됩니다.
아주 좋은 대답, 타이.나는 다른 대답들에서 아이디어를 얻었지만 이것은 훨씬 더 설명 적이다. – Claudiu