本帖最后由 狼王 于 2013-7-15 20:45 编辑
- class Singleton{//<span style="color: rgb(63, 127, 95); font-family: 宋体; font-size: 10.5pt; mso-bidi-font-family: Consolas; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA;">饿汉式:</span>
- private int num;
- public int getNum() {
- return num;
- }
- public void setNum(int num) {
- this.num = num;
- }
- private Singleton(){}
- private static Singleton s = new Singleton();
- public static Singleton getInstance(){
- return s;
- }
- }
复制代码 区别:饿汉式先定义对象
懒汉式:对象的延时加载,而是还有线程安全问题 |