java单例类
我有一个单例父类,由一个不是单例的子类扩展。如果我多次实例化我的子类,是否会有多个父类对象。请澄清。
我的意思是,基本上,我想知道如果实例化了子类,是否也会创建父类的新实例。请解释一下。
I have a singleton parent class extended by a child class which is not singleton. If i instantiate my child class more than once, will there be more than one object of parent class. Please clarify.
I mean, basically, i want to know if a child class is instantiated, will a new instance of the parent class will also be created. Please explain.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当你实例化一个类时,你不会实例化它的任何父类。所以从技术上讲,哪里不会有单例类的实例。
但是,如果您的“单例”类可以被继承(即它不是最终的),那么它就不是真正的单例。
例如,它的所有成员也将在子代中,因此可能会有多个实例 - 因此它并不是真正的单例。
When you instantiate a class you don't instantiate any of its parents. So technically where will be no instances of the singleton class.
However, if your 'singleton' class can be inherited (i.e. it's not final), then it is not really a singleton.
For example all the members of it will be also in the child, so there will potentially be more than one instance of them - so it's not really a singleton.