DIFF。在单例类和静态类之间 [英] Diff. between singleton class and static class

查看:76
本文介绍了DIFF。在单例类和静态类之间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


什么是差异。在C#中的单例类和静态类之间?

Hi All,

What is the diff. between a singleton class and a static class in C#?

推荐答案

静态类不允许任何东西创建自己的实例

除了它自己 - 它没有公共ctors:


class T

{

private T() {}


public static T Get(){return new T(); }

};


T t1 = new T(); //错误,没有公共构造函数

T t2 = T.Get(); // Ok

T t3 = T.Get(); // 好;返回T的另一个实例

单身人士只允许创建一个类的实例:


class S

{

private S(){}


private static S instance = new S();


public static S Get(){return S.instance; }

};


S s1 = new S(); //错误,没有公共构造函数

S s2 = S.Get(); // 好;总是返回相同的实例

S s3 = S.Get(); // 好;返回与s2相同的实例


" DBA"写道:
A static class does not allow an instance of itself to be created by anything
other than itself - it has no public ctors:

class T
{
private T() { }

public static T Get() { return new T(); }
};

T t1 = new T(); // Error, no public constructor
T t2 = T.Get(); // Ok
T t3 = T.Get(); // Ok; returns another instance of T

A singleton allows only one instance of the class to ever be created:

class S
{
private S() { }

private static S instance = new S();

public static S Get() { return S.instance; }
};

S s1 = new S(); // Error, no public constructor
S s2 = S.Get(); // Ok; always returns the same instnace
S s3 = S.Get(); // Ok; returns the same instance as s2


"DBA" wrote:
大家好,

什么是差异。单例类和C#中的静态类之间?
Hi All,

What is the diff. between a singleton class and a static class in C#?



DBA< DB*@discussions.microsoft.com>写道:
DBA <DB*@discussions.microsoft.com> wrote:
什么是差异。单例类和C#中的静态类之间?
What is the diff. between a singleton class and a static class in C#?




单例允许访问单个创建的实例 - 该实例

(或者更确切地说) ,对该实例的引用)可以作为参数

传递给其他方法,并作为普通对象处理。


静态类只允许静态方法。


-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet

如果回复群组,请不要给我发邮件



A singleton allows access to a single created instance - that instance
(or rather, a reference to that instance) can be passed as a parameter
to other methods, and treated as a normal object.

A static class allows only static methods.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too





首先目前没有办法声明一个类静态,你通过声明静态的所有成员来隐含地实现
。只要Appdomain这样做,成员就会存在



单身是一个正常的唯一的区别是

构造函数是私有的,因此是创建的唯一方法。一个实例正在使用

返回实例的属性/方法。如果需要,你也可以声明

a方法来处理实例,所以当需要

时它可以重新创建。

欢呼,

-

Ignacio Machin,

ignacio.machin at dot.state.fl.us

Florida交通部


" DBA" < DB*@discussions.microsoft.com>在消息中写道

新闻:D4 ********************************** @ microsof t.com ...
Hi,

first the currently there is no a way to declare a class static, you do it
implicitely by declaring static all the members of it. The members will
exist as long as the Appdomain does.

A singleton is a "normal" class with the only difference that the
constructor is private, hence the only way to "create" an instance is using
the property/method that return the instance. If needed you can also declare
a method that dispose the instance, so it can be recreated again when
needed.
cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"DBA" <DB*@discussions.microsoft.com> wrote in message
news:D4**********************************@microsof t.com...
大家好,

什么是差异。单例类和C#中的静态类之间?
Hi All,

What is the diff. between a singleton class and a static class in C#?



这篇关于DIFF。在单例类和静态类之间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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