什么是好?静态方法或实例方法 [英] What is better? Static methods OR Instance methods

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

问题描述

我发现有两种类型的称为静态方法和实例方法和分歧的方法。
但我仍然不可能了解一个比另一个优势。

I found that there are two type of methods called static methods and instance methods and their differences. But still I couldnt understand the advantages of one over another.

有时我觉得,静态方法不是100%面向对象的。

Sometimes i feel that static methods are not 100% object oriented.

是否有这两者之间的任何性能差异。

Are there any performance differences between this two.

有人能帮助?

推荐答案

在一个完美的世界OO有可能不会是任何需要静态方法(我想艾菲尔没有他们,无论是)。但在这一天什么事情的到底是不是你的code的面向对象纯度(C#有足够的概念不是严格的纯面向对象,如扩展方法,例如),而是你要做些什么。

In a perfect OO world there probably wouldn't be any need for static methods (I think Eiffel doesn't have them, either). But at the end of the day what matters is not OO-pureness of your code (C# has enough concepts that aren't strictly pure OO, like extension methods, for example) but rather what you're getting done.

您可以使用一般的辅助方法静态方法(即不需要对自己的一般辅助类或状态)或之类的东西Color.FromARGB(),它的行为略有构造器一样的值类型。

You can use static methods for general helper methods (that don't need a general helper class or state on their own) or things like Color.FromARGB() which behave slightly contructor-like for value types.

在一般情况下,不触及一个对象的状态的任何方法(因此是更类特定比特定对象)可以由静态的。性能差异真的不应该出现的。不太可测,在任何情况下。扬灰的大文​​章写入速度更快管理code:知道什么东西的成本有一些硬数据在此,尽管采取谨慎。

In general, any method that doesn't touch an objects state (and therefore is more class-specific than object-specific) can be made static. Performance differences shouldn't really arise. Not very measurable, in any case. Jan Gray's great article Writing faster managed code: Know what things cost has some hard data on this, albeit to be taken with care.

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

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