1
爲什麼以下程序的輸出在定稿()之前跑Classmate運行結束? Classmate使用課程對象,因此它的finalize()應該在課程的finalize()之後運行?但輸出顯示反向.WHY?輸出定稿在構圖不確定
class Classmate{
Course mca;
Classmate(){
System.out.println("Student const. `enter code here`called");
mca = new Course();
mca.getCourse();
}
@Override
protected void finalize() {System.out.println("good bye Student");
}
}
class Course{
Course(){
System.out.println("Course const. called");
}
void getCourse(){
System.out.println("your ccourse is MCA");
}
@Override
protected void finalize() throws Throwable {
// TODO Auto-generated method stub
System.out.println("goodbye course");
}
}
public class Composition {
public static void main(String[] args) {
Classmate ram = new Classmate();
ram=null;
System.gc();
for(int i=0;i<5;i++)
System.out.println("i is "+i);
}
}
OUTPUT:
Student const. called
Course const. called
your ccourse is MCA
good bye Student
i is 0
goodbye course
i is 1
i is 2
i is 3
i is 4
輸出學生用於代替Classmate –