天天看点

java 枚举法单例模式_JAVA三种实现单例模式方法(三):枚举式实现单例设计模式...

package instance.enumStyle;

public class InstanceEnumStyle {

private InstanceEnumStyle() {}

public static InstanceEnumStyle getInstance() {

return Singleton.INSTANCE.getSingle();

}

private static enum Singleton {

INSTANCE;

private InstanceEnumStyle instance;

private Singleton () {

instance = new InstanceEnumStyle();

}

public InstanceEnumStyle getSingle() {

return instance;

}

}

public static void main(String[] args) {

for (int i = 0; i < 3; i++) {

new Thread(new Runnable() {

@Override

public void run() {

InstanceEnumStyle instanceEnumStyle = InstanceEnumStyle.getInstance();

System.out.println("hashCode:" + instanceEnumStyle.hashCode());

}

}).start();

}

}

}

测试结果:

hashCode:1575346308

hashCode:1575346308

hashCode:1575346308

更多内容:

https://www.cnblogs.com/linjiaxin/p/7923135.html