静态类方法返回非静态对象? [英] Static class method return non-static object?

查看:103
本文介绍了静态类方法返回非静态对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我有一个类,它的所有属性,属性和

方法都是静态的。


但是,我想要一个方法来返回一个应该是非b = b
的对象。


这可能吗?

thx,

Pascal

解决方案




这是完全可能的,例如,当静态属性返回

同一类的实例时,用于实现

Singleton设计模式。 br />

-

此致,

Dmitriy Lapshin [C#/ .NET MVP]

带来今天对VS .NET IDE进行单元测试的能力!
http: //www.x-unity.net/teststudio.aspx

" Paschalis Pagonidis" <一个******* @ discussions.microsoft.com>在消息中写道

news:4a **************************** @ phx.gbl ... < blockquote class =post_quotes>

我有一个类,它的所有属性,属性和
方法都是静态的。

但是,我想要一个方法返回一个应该是非静态的对象。

这可能吗?

thx,
Pascal




Paschalis Pagonidis< an ******* @ discussion.microsoft.com>写道:

我有一个类,其所有的属性,属性和
方法都是静态的。

然而,我想要一个方法来返回一个应该<的对象是不可能的。

这可能吗?




对象本身不是静态的或非静态的 - 概念只是

不适用。你能详细介绍一下你想要做什么吗?

-

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

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


嗨Dmitriy,


你能给我一个例子吗?


例如:

公共类MyClass

{

公共静态对象CreateObject()

{

//我希望obj是非静态的

对象obj = new object();


返回obj;

}


谢谢,

Pascal

-----原始消息-----


那是完全可能的,是例如,当静态属性返回
同一类的实例时,用于
实现的单独设计模式。

-
真诚,Dmitriy Lapshin [C#/ .NET MVP]
今天将单元测试的强大功能带到VS .NET IDE中!
http://www.x-unity.net/teststudio.aspx

Paschalis Pagonidis
< an ******* @ discussion.microsoft.com>在messagenews中写道:4a **************************** @ phx.gbl ...



我有一个类,它的所有属性,属性和
方法都是静态的。

但是,我想要一个方法来返回一个应该
是非静态的。

这可能吗?

thx,
Pascal





Hi,

I have a class which all its attributes, properties and
methods are static.

However, I want a method to return an object that should
be non-static.

Is that possible?

thx,
Pascal

解决方案

Hi,

That''s perfectly possible and is, for example, used in implementation of the
Singleton design pattern when a static property returns an instance of the
same class.

--
Sincerely,
Dmitriy Lapshin [C# / .NET MVP]
Bring the power of unit testing to the VS .NET IDE today!
http://www.x-unity.net/teststudio.aspx

"Paschalis Pagonidis" <an*******@discussions.microsoft.com> wrote in message
news:4a****************************@phx.gbl...

Hi,

I have a class which all its attributes, properties and
methods are static.

However, I want a method to return an object that should
be non-static.

Is that possible?

thx,
Pascal




Paschalis Pagonidis <an*******@discussions.microsoft.com> wrote:

I have a class which all its attributes, properties and
methods are static.

However, I want a method to return an object that should
be non-static.

Is that possible?



Objects themselves aren''t static or non-static - the concept just
doesn''t apply. Could you give more details about what you''re trying to
do?

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


Hi Dmitriy,

can you give me an example?

for example:
public class MyClass
{
public static object CreateObject()
{
// I want obj to be non-static
object obj = new object();

return obj;
}

thanks,
Pascal

-----Original Message-----
Hi,

That''s perfectly possible and is, for example, used in implementation of theSingleton design pattern when a static property returns an instance of thesame class.

--
Sincerely,
Dmitriy Lapshin [C# / .NET MVP]
Bring the power of unit testing to the VS .NET IDE today!
http://www.x-unity.net/teststudio.aspx

"Paschalis Pagonidis" <an*******@discussions.microsoft.com> wrote in messagenews:4a****************************@phx.gbl...

Hi,

I have a class which all its attributes, properties and
methods are static.

However, I want a method to return an object that should
be non-static.

Is that possible?

thx,
Pascal



.



这篇关于静态类方法返回非静态对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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