静态方法? [英] Static methods ?

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

问题描述

你好,


i想知道类中静态方法的用法。

在类中使用更多静态方法是有利还是缺点。


谢谢你

hello,

i want to know usage of static methods in a class.
is it advantageous or disadvantage to use more static methods in a class.

thank u

推荐答案

chandu写道:
chandu wrote:

你好,


i想知道类中静态方法的用法。

在类中使用更多静态方法是有利还是缺点。
hello,

i want to know usage of static methods in a class.
is it advantageous or disadvantage to use more static methods in a class.



当执行计算(逻辑)所需的所有信息都可以作为参数传递给静态方法时非常有用

方法。因此,该方法不需要任何对象的状态(字段)

来完成它的工作。


我更喜欢静态方法我有一些商业逻辑/计算

,只需要几条信息(参数),因为当

我看到关键字静态时我知道我不必阅读方法

来查看它使用(或修改)对象状态的哪些部分。

一切都在参数列表。


(当然,一个静态方法可以利用静态,但是后来IMHO

a类带有多个静态项州开始看起来好像b $ b更像是一个应该是单身的东西。)


那就是用大量静态方法编程并避免

实例方法有点结构化编程......有些人没有b $ b毕业于面向对象的世界。很多时候作为静态方法写的

实际上属于

参数''类型之一的实例方法。例如,一个静态方法,它将你的参数作为一个你所写的类的一个b / b $ b ...你应该考虑

使它成为一个实例方法的可能性上课并看看

是否更有意义。调用myString.Trim()而不是

String.Trim(myString)更好,即使两种实现都是可能的。我通常会问自己,这个静态方法真的是对某些

对象的操作吗?有时答案是肯定的,有时它是否定的。


与任何其他工具一样,静态方法也有其用途。不要害怕他们,但他们也不要过度使用它们。

Static methods are useful when all of the information needed in order
to perform a calculation (logic) can be passed as arguments to the
method. So, the method does not need any of the object''s state (fields)
in order to do its work.

I prefer static methods when I have some business logic / calculation
that needs only a few pieces of information (parameters), because when
I see the keyword "static" I know that I don''t have to read the method
to see what bits of the object''s state it uses (or modifies).
Everything is in the parameter list.

(Of course, a static method can make use of static state, but then IMHO
a class with more than a few items of static state is starting to look
more like something that should be a singleton.)

That said, programming with lots of static methods and avoiding
instance methods smacks of structured programming... someone who hasn''t
quite graduated into the object-oriented world. Many times what are
written as static methods really belong as instance methods on one of
the parameters'' types. For example, a static method that takes as one
of its parameters a class that you wrote... you should consider the
possibility of making it an instance method of that class and see if
that makes more sense. It''s nicer to call myString.Trim() rather than
String.Trim(myString), even though both implementations are possible. I
usually ask myself, "Is this static method really an operation on some
object?" Sometimes the answer is yes, sometimes it''s no.

Like any other tool, static methods have their uses. Don''t be afraid of
them, but then don''t overuse them, either.


" chandu" ; < na ************* @ hotmail.comwrote in message

news:uF ************** @ TK2MSFTNGP05。 phx.gbl ...
"chandu" <na*************@hotmail.comwrote in message
news:uF**************@TK2MSFTNGP05.phx.gbl...

hello,


i想知道类中静态方法的用法。

在类中使用更多静态方法是有利还是缺点。
hello,

i want to know usage of static methods in a class.
is it advantageous or disadvantage to use more static methods in a class.



取决于课程,有些课程只有静态方法,而其他课程则没有.b $ b没有。与任何功能一样,使用它也是有利的。

Depends on the class, some classes have only static methods while others
have none. Like any feature it''s advantageous to use it appropriately.


>

谢谢你
>
thank u



静态方法性价比,但可以是线程安全的。

静态方法保留其先前的状态


我会使用静态方法,当...

我需要允许其他模块,系统访问一个方法,而不是创建一个类的实例。

我需要制作一个存储其previuos状态的方法

当我处理多线程应用程序时

当Impliementing Singleton和工厂模式时

当我需要在多个系统中共享许多公共方法时,每个系统可能会或可能不会使用该类的所有方法


Nirosh。


" chandu" < na ************* @ hotmail.comwrote in message

news:uF ************** @ TK2MSFTNGP05。 phx.gbl ...
Static method cost performance, but can be thread safe.
Static method preserve its previous state

I would use static method, when ...
I need to issolately allow other modules, systems to access a method without
creating a instance of a class.
I need to make a mehtod that store its previuos status
When I deal with multithreaded application
When Impliementing Singleton and factory pattern
When I need to share many public methods accross multiple systems where each
system may or may not use all the method of the class

Nirosh.

"chandu" <na*************@hotmail.comwrote in message
news:uF**************@TK2MSFTNGP05.phx.gbl...

hello,


i想知道类中静态方法的用法。

在类中使用更多静态方法是有利还是不利。


谢谢你
hello,

i want to know usage of static methods in a class.
is it advantageous or disadvantage to use more static methods in a class.

thank u



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

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