Simple screenshot that explains the singleton invocation.
2024-10-19 01:18:54
Here is the code:
/*
Some class,such as a config file,need to be only one.So we need to control the instance.
1,private the constructor and create only one instance in the class itself.
2,provide a method for the others to get the 'only one' instance.
*/
package kju.obj; import static kju.print.Printer.*;
public class SingletonDemo {
public static void main(String[] args) {
SingleConfig con01 = SingleConfig.getInstance();
SingleConfig con02 = SingleConfig.getInstance();
println("con01 color : " + con01.getColor());
println("con02 set color : ");
con02.setColor("Blue");
println("con01 color : " + con01.getColor());
/*
con01 color : Orange
con02 set color :
con01 color : Blue
*/
}
} class SingleConfig {
private String color = "Orange";
private static SingleConfig s = new SingleConfig(); private SingleConfig() {}
public static SingleConfig getInstance() {
return s;
} public void setColor(String color) {
this.color = color;
} public String getColor() {
return color;
}
}
And the corresponds the code above:
最新文章
- SpringMVC无法获取请求中的参数的问题的调查与解决(2)
- Chrome开发者工具详解(4)-Profiles面板
- jsp中frameset frame不显示页面
- 151008:javascript不明白的地方
- Force.com微信开发系列(七)OAuth2.0网页授权
- 【Unity3D】Unity3D之 注册表动态存取游戏存档——PlayerPrefs类
- 检测网络变化(wifi、2g、3g、4g)
- [kmp+dp] hdu 4628 Pieces
- 去掉谷歌input记住账号或密码时默认出现的黄色背景
- asp.net web api 构建api帮助文档
- 前端笔记知识点整合之JavaScript(二)关于运算符&;初识条件判断语句
- C#与SQL Server数据库连接
- Activity与Fragment数据传递之Fragment从Activity获取数据
- 1.rabbitmq高可用方案
- 多任务Forth系统内存布局
- 1552/3506. [CQOI2014]排序机械臂【平衡树-splay】
- linux缓存手动清理
- CentOS7.2 安装nginx-1.10.3
- PAT 甲级 1050 String Subtraction
- java 接口转码、加密