如何多次实例化一个Singleton?

如何多次实例化一个Singleton?,第1张

如何多次实例化一个Singleton?

您可以使用反射来调用单例类的私有构造函数,以创建该类的新实例。

class MySingleton {    private MySingleton() {    }}class Test {    public void test() throws Exception {        Constructor<MySingleton> constructor = MySingleton.class.getConstructor();        constructor.setAccessible(true);        MySingleton otherSingleton = constructor.newInstance();    }}


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5500099.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-12-12
下一篇2022-12-12

发表评论

登录后才能评论

评论列表(0条)

    保存