何时适合使用静态方法? [英] When is it appropriate to use static methods?

查看:70
本文介绍了何时适合使用静态方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这不是直观明确的。

This is not intuitivelly clear.

推荐答案

Tom,


静态方法是做的方法不分享任何变数。这是我们在课外的VB6子程序相同的




示例:

class Foo

{

private string thisTest =" test" ;;

public string MyString()

{

返回thisTest;

}


public static MyStaticString()

{

return 测试;

}

}


用法:


时使用静态方法你可以调用

string test = Foo.MyStaticString();


如果它不是静态的你必须创建和实例的class


Foo myFoo = new Foo();

string test = myFoo.MyString();


基本上所有归结为内存使用。一个静态方法是共享的,

为每个类的实例创建一个非静态方法。

希望这有意义吗?

汤姆 < nf*@nospam.com>在消息中写道

新闻:%2 **************** @ TK2MSFTNGP10.phx.gbl ...
Tom,

Static methods are methods that do not share any variables. the are the
same as VB6 sub routines that our outside of a class.

Example:
class Foo
{
private string thisTest = "test";
public string MyString()
{
return thisTest;
}

public static MyStaticString()
{
return "test";
}
}

usage:

when using a static method you can just call
string test = Foo.MyStaticString();

if it is not static you have to create and instance of the class

Foo myFoo = new Foo();
string test = myFoo.MyString();

Basically is all boils down to memory usage. a static method is shared,
where as a non-static method gets created for every instance of the class.
Hope this makes sense?
"Tom" <nf*@nospam.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
这是不直观清晰。
This is not intuitivelly clear.



当方法不属于特定对象时使用静态方法。


例如,如果你看一下.NET框架中的Math类,你会看到所有方法都是静态的

。为什么?因为没有理由必须创建

对象来使用这些方法。为什么你想要创建一个

Math类的对象,当你想要的只是某个东西的绝对值时?不,那里

没有理由这样做,因此,这个方法是静态的。


所以当你设计课程时,问问自己:


这个方法属于一个对象,还是类本身?


一个方法属于一个对象,如果它修改了对象的状态。如果

方法不修改特定对象,它很可能是静态的。


另一个例子,假设你想知道多少个对象创建一个类

(不要问我为什么......)。对于这个任务,你可以创建一个

静态方法GetNumberOfObjects()(你显然需要一个静态字段,

和构造函数中的一些代码)。为什么我会让它静止,你可能会问b $ b。好吧,回答上面的问题,你会看到。该方法不属于
属于任何特定对象。此外,它不会修改任何对象。


我希望这是有道理的。


-

问候,

Kristofer Gafvert - IIS MVP
http:// www。 ilopia.com - 当您需要帮助时!

" Tom" < nf*@nospam.com>在消息中写道

新闻:%2 **************** @ TK2MSFTNGP10.phx.gbl ...
Use a static method when the method does not belong to a specific object.

For example, if you look at the Math class in .NET framework, you will see
that all methods are static. Why? Because there is no reason to must create
an object to use the methods. Why would you want to create an object of the
Math class, when all you want is the absolute value of something? No, there
is no reason to do this, and therefore, the method is static.

So when you design a class, ask yourself:

Does this method belong to an object, or the class itself?

A method belongs to an object, if it modifies the state of the object. If
the method does not modify a specific object, it can most likely be static.

Another example, suppose that you want to know how many objects of a class
that is created (don''t ask me why...). For this task, you could create a
static method GetNumberOfObjects() (and you obviously need a static field,
and some code in the constructor too). Why would i have it static, you might
ask. Well, answer the above question, and you will see. The method does not
belong to any specific object. Additionally, it does not modify any object.

I hope this makes sense.

--
Regards,
Kristofer Gafvert - IIS MVP
http://www.ilopia.com - When you need help!
"Tom" <nf*@nospam.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
这是不直观清楚。
This is not intuitivelly clear.





" Kristofer Gafvert" <千克****** @ NEWSilopia.com>在消息中写道

news:u%**************** @ TK2MSFTNGP09.phx.gbl ...

"Kristofer Gafvert" <kg******@NEWSilopia.com> wrote in message
news:u%****************@TK2MSFTNGP09.phx.gbl...
使用当方法不属于特定对象时的静态方法。
Use a static method when the method does not belong to a specific object.




如果该类是单例怎么办?


For例如,我有一个WinForm应用程序,它不允许应用程序的多个

实例在同一个工作站上运行。这个

应用程序使用一个缓存全局变量的业务对象单例,

,例如对多个MDI子项可以使用的DataSet的引用

表格。


每次我想使用其中一种方法时,创建一个新实例并不是特别有用或高效

单身。



What if the class is a singleton?

For instance, I have a WinForm application that does not allow multiple
instances of the application to run on the same workstation. This
application uses a Business Object singleton that caches global variables,
such as a reference to a DataSet that can be used by multiple MDI child
forms.

It doesn''t seem particularly useful or efficient to create a new instance
every time I want to use one of the methods in this singleton.


这篇关于何时适合使用静态方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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