今天完成了昨天的程序
改完了那个程序
在此附上代码和截图
Stundent类的代码
package a20200723;
public class Student {
private String name;
private String no;
private int age;
private static int sum=0;
public String getNo() {
return no;
}
public String getName() {
private String name;
private String no;
private int age;
private static int sum=0;
public String getNo() {
return no;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public void setNo(String no) {
this.no = no;
}
public void setName(String name) {
public void setNo(String no) {
this.no = no;
}
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public Student() {
this.no=null;
this.name=null;
this.age=0;
sum=sum+1;
System.out.print("Constructor run ");
System.out.print("NumTotal:"+sum+" ");
}
this.no=null;
this.name=null;
this.age=0;
sum=sum+1;
System.out.print("Constructor run ");
System.out.print("NumTotal:"+sum+" ");
}
public Student(String no,String name, int age) {
this.name = name;
this.age = age;
sum=sum+1;
System.out.print("Constructor run ");
System.out.print("NumTotal:"+sum+" ");
}
sum=sum+1;
System.out.print("Constructor run ");
System.out.print("NumTotal:"+sum+" ");
}
public void show(){
System.out.println("我叫"+name+",我今年"+age+"岁
");
}
pubhli'zc v'yoizd finalize() {
sum=sum-1;
System.out.print("Destructor run ");
System.out.print("NumTotal:"+sum+" ");
}
pubhli'zc v'yoizd finalize() {
sum=sum-1;
System.out.print("Destructor run ");
System.out.print("NumTotal:"+sum+" ");
}
}
主函数代码
package a20200723;
import a20200723.Student;
public class main {
public static void main(String[] args)
{
String s1="20190327";
String s2="doublebest";
Student stu1=new Student(s1,s2,0);
stu1.setAge(21);
stu1.show();
Student stu2=new Student();
stu2=stu1;
stu2.setNo(s1);
stu2.setName(s2);
stu2.show();
stu1=null;
stu2=null;
System.gc();
}
}
import a20200723.Student;
public class main {
public static void main(String[] args)
{
String s1="20190327";
String s2="doublebest";
Student stu1=new Student(s1,s2,0);
stu1.setAge(21);
stu1.show();
Student stu2=new Student();
stu2=stu1;
stu2.setNo(s1);
stu2.setName(s2);
stu2.show();
stu1=null;
stu2=null;
System.gc();
}
}
截图