public class Singleton {

private static Singleton instance;

private Singleton() { // 私有构造函数,防止外部实例化 }

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

public void showMessage() { System.out.println('Hello, World!'); } }

Java 单例模式实现详解:代码示例与最佳实践

原文地址: https://www.cveoy.top/t/topic/npPK 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录