- class Single{
- //饿汉式
- private static Single s=new Single();
- private Single(){}
- public static Single getInstance(){
- return s;
- }
- }
- //懒汉式
- private static Single s=null;
- private Single(){}
- public static Single getInstance(){
- if(s==null)
- Single s=new Single();
- return s;
- }}
- public static void main (String []args){
- Single ss=Single.getInstance();
- }
复制代码 |
|