类图
源码
package com.test; public class StuNo { private static StuNo instance=null; private String no; private StuNo() { } public static StuNo getInstance() { if(instance==null) { System.out.println("入学,分配学号!"); instance=new StuNo(); instance.setStuNo("20193922"); } else { System.out.println("返校,获取旧号码!"); } return instance; } private void setStuNo(String no) { this.no=no; } public String getStuNo() { return this.no; } } Client.java package com.test; public class Client { public static void main(String a[]) { StuNo no1,no2; no1=StuNo.getInstance(); no2=StuNo.getInstance(); System.out.println("学号是否一致:" + (no1==no2)); String str1,str2; str1=no1.getStuNo(); str2=no1.getStuNo(); System.out.println("第一次学号:" + str1); System.out.println("第二次学号:" + str2); System.out.println("内容是否相等:" + str1.equalsIgnoreCase(str2)); System.out.println("是否是相同对象:" + (str1==str2)); } }
测试截图