写一个SingleTon,(饿最终、懒同步)

1.饿汉式:

public class SingleTon {

    private SingleTon(){

    }

    private final static SingleTon instance = new SingleTon();

    public static SingleTon getInstance(){
return instance;
}
}

2.懒汉式:

public class SingleTon {

    private SingleTon(){

    }

    private static SingleTon instance = null;

    public static synchronized SingleTon getInstance(){
if(instance == null)
instance = new SingleTon();
return instance;
}
}

3.枚举:

public enum SingleTon {
instance;
}
上一篇:Java 基础 - 基本类型和引用类型


下一篇:你真的会玩SQL吗?和平大使 内连接、外连接