ibeautiful
package singleton;import java.lang.reflect.Constructor;import java.lang.reflect.InvocationTargetException;//AprilCal on 2016.4.23class Singleton//最普通的一种Singleton实现{private static Singleton INSTANCE=new Singleton();private Singleton(){System.out.println("私有构造函数被调用");}public static Singleton getInstance(){return INSTANCE;}}enum EnumSingleton2//枚举实现的Singleton{INSTANCE;private EnumSingleton2(){System.out.println("enum 私有构造函数被调用");}}public class SingletonTest{public static void main(String[] args)throws ClassNotFoundException, IllegalAccessException,IllegalArgumentException, InvocationTargetException,NoSuchMethodException, SecurityException, InstantiationException{Class <?> cls1 = Class.forName("singleton.Singleton");Class <?> cls2 = Class.forName("singleton.EnumSingleton2");//通过反射调用Singleton的构造函数Constructor <?> c0=cls1.getDeclaredConstructor();c0.setAccessible(true);Singleton s=(Singleton)c0.newInstance();//通过反射调用EnumSingleton的构造函数Constructor <?> c1=cls2.getDeclaredConstructor();c1.setAccessible(true);EnumSingleton es=(EnumSingleton)c1.newInstance();}}