java单例类

发布于 2024-11-19 22:07:19 字数 108 浏览 1 评论 0原文

我有一个单例父类,由一个不是单例的子类扩展。如果我多次实例化我的子类,是否会有多个父类对象。请澄清。

我的意思是,基本上,我想知道如果实例化了子类,是否也会创建父类的新实例。请解释一下。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情话已封尘 2024-11-26 22:07:19

当你实例化一个类时,你不会实例化它的任何父类。所以从技术上讲,哪里不会有单例类的实例。

但是,如果您的“单例”类可以被继承(即它不是最终的),那么它就不是真正的单例。

例如,它的所有成员也将在子代中,因此可能会有多个实例 - 因此它并不是真正的单例。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文