java 基础之枚举

1.在一个项目中多个方法中都用到了外部的配置文件,所以想写一个单例模式来读取一次外部配置文件,而不是每次用的时候都读一次

枚举实现的单例模式,其他模式见 http://www.cnblogs.com/predisw/p/4763513.html

public enum SingletonPropsByEnum {
    INSTANCE;
    
    private Properties properties;
    private InputStream in;
    
    private SingletonPropsByEnum(){
        in = this.getClass().getResourceAsStream("/System.properties");  //注意,这里不能改成参数的形式!!!,因为会初始化一次.
        properties = new Properties();                                    
    }
    
    public Properties getProperties() throws IOException{
        properties.load(in);
        return properties;
    }
}

 2.枚举类型为什么是线程安全的呢?

3.枚举使用的一些特性

posted @ 2015-08-27 16:40  predisw  阅读(137)  评论(0编辑  收藏  举报