单例模式(Singleton):保证一个类仅有一个实例,并提供一个访问它的全局访问点。 类结构图: Singleton.java package com.china_chenglong; public class Singleton { private static Singleton instance; private Singleton(){}; public static Singleton GetInstance(){ if(instance == null){ instance = new Singleton(); } return instance; } } Main.java package com.china_chenglong; public class Main { public static void main(String[] args) { Singleton s1 = Singleton.GetInstance(); Singleton s2 = Singleton.GetInstance(); if(s1 == s2){ System.out.println("两个对象都相等"); } } }