学号的单一
仿照课堂的身份证的例子,实现每个同学仅有一个学号这一问题。
Client:
package 实验7;
public class Client {
public static void main(String a[]) {
StudentID stu1,stu2;
String id1,id2;
System.out.println("第一次生成学生");
stu1 = StudentID.getInstance();
System.out.println("第二次生成学生");
stu2 = StudentID.getInstance();
System.out.println("两学生学号是否一致:" + (stu1==stu2));
id1 = stu1.getStudentID();
id2 = stu1.getStudentID();
System.out.println("第一次产生的学号:" + id1);
System.out.println("第二次产生的学号:" + id2);
System.out.println("内容是否相等:" + id1.equalsIgnoreCase(id2));
System.out.println("是否是相同对象:" + (id1==id2));
}
}
StudentID:
package 实验7;
public class StudentID {
private static StudentID instance = null;
private String id;
private StudentID(){}
public static StudentID getInstance() {
if (instance == null) {
System.out.println("学生信息注册,分配学号!");
instance = new StudentID();
instance.setStudentID("20213961");
} else {
System.out.println("学生信息已经被注册,获取其已经存在的学号!");
}
return instance;
}
private void setStudentID(String id) {
this.id = id;
}
public String getStudentID() {
return this.id;
}
}
标签:总结,StudentID,学号,每日,System,11.02,println,id,out From: https://www.cnblogs.com/jm56/p/17804642.html