例9.3 析构函数
1 public class cjava { 2 public static void main(String[] args) { 3 Student s1=new Student(10010,"wang ba",'f'); 4 s1.display(); 5 Student s2=new Student(10011,"di di",'m'); 6 s2.display(); 7 s1.finalize(); 8 s2.finalize(); 9 } 10 } 11 class Student{ 12 int num; 13 char sex; 14 String name; 15 Student(int n,String nam,char s){ 16 num=n; 17 name=nam; 18 sex=s; 19 System.out.println("Constructor called"); 20 } 21 protected void finalize(){ 22 System.out.println("Destructor called"); 23 } 24 void display() { 25 System.out.println("num: "+num); 26 System.out.println("name: "+name); 27 System.out.println("sex: "+sex); 28 } 29 }
2.java没有析构函数,但是有finalize()替代
3.明天继续写例题