欢迎来到杳无音讯的个人站|

杳无音讯

园龄:2年11个月粉丝:9关注:9

实验7:单例模式

实验7:单例模式

本次实验属于模仿型实验,通过本次实验学生将掌握以下内容:

1、理解单例模式的动机,掌握该模式的结构;

2、能够利用单列模式解决实际问题。

 

[实验任务一]:学号的单一

仿照课堂的身份证的例子,实现每个同学仅有一个学号这一问题。

实验要求:

1. 画出对应的类图;

 

 

2. 源代码及运行截图;

 

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
Client
package com.hua.sjms; 
public class client { 
public static void main(String[] args) { 
StudentID stu1,stu2; 
stu1=StudentID.getStudentID(); 
stu2=StudentID.getStudentID(); 
String str1,str2; 
str1=stu1.getID(); 
str2=stu2.getID(); 
System.out.println("第一次学号:"+str1); 
System.out.println("第二次学号:"+str2); 
package com.hua.sjms; 
public class StudentID { 
private static StudentID instance = null
private String ID; 
public String getID() { 
return ID; 
public void setID(String iD) { 
ID = iD; 
private StudentID() { 
public static StudentID getStudentID() { 
if (instance == null) { 
instance = new StudentID(); 
instance.setID("20214165"); 
} else
System.out.println("一个学生只能有一个学号"); 
return instance; 
}

  

 

 

本文作者:杳无音讯

本文链接:https://www.cnblogs.com/130-gzh/p/17856268.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   杳无音讯  阅读(12)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起