等级Vs. SingletonClass [英] Class Vs. SingletonClass

查看:72
本文介绍了等级Vs. SingletonClass的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



普通班级与Singleton班级有什么不同?

有一个场景我被面试官问到了.......



如何将普通班级转换成单身班级...... !!!!



我在我的项目中使用过Singleton Class但是我找不到普通类和单例之间的任何区别。



任何人都可以帮忙我这个。



提前谢谢。

Hi All,

How normal class is different from Singleton class?
There is a scenario which i was asked by an interviewer.......

How will you convert normal class into a singleton class......!!!!

I have used Singleton Class in my project but i could not find any difference between normal class and singleton.

Could any one help me on this.

Thanks in advance.

推荐答案

最大的区别(确实在代码方面很漂亮)很多唯一的区别)是Singleton类没有公共构造函数:你必须通过类GetInstance方法(或类似的)来创建一个实例,因为不允许外部世界使用:

The biggest difference (indeed in code terms pretty much the only difference) is that a Singleton class does not have a public constructor: you have to go via the class GetInstance method (or similar) in order to create an instance as the outside world is not allowed to use:
MySingletonClass msc = new MySingletonClass();

必须使用

It must use

MySingletonClass msc = MySingletonClass.GetInstance();

这确保了只有一个类的实例。

Which ensures that there is only ever one instance of the class.


这篇关于等级Vs. SingletonClass的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆