问题描述
我有一个跟踪基因的 Gene
类。 Gene
有一种计算两个基因之间距离的方法。有没有理由让它静止?
I have a Gene
class that keeps track of genes. Gene
has a method for calculating the distance between two genes. Are there any reasons to make it static?
哪个更好?
public static int geneDistance(Gene g0,Gene g1)
或
public int geneDistance(Gene other)
支持/反对使其静态的参数?我理解成员静态意味着什么,我只关心其对最大清洁度/效率等的影响。
Arguments for/against making it static? I understand what it means for a member to be static, I'm just interested in its implications for maximum cleanliness/efficiency/etc.
我重复相同的模式返回修剪两个基因的版本,找到基因之间的匹配,找到动物之间的匹配(包含基因集合)等。
I repeat the same pattern for returning trimmed versions of two genes, finding matches between genes, finding matches between animals (which contain collections of genes), etc.
推荐答案
实例,而非静态
对于这种情况,我认为第二种选择显然更好。如果你考虑一下,如果你愿意将对象传递给它,任何方法都可以实现为静态,只有似乎是一个特例,因为另一个参数也是一个实例。
Instance, not static
For this case I think the second choice is clearly better. If you think about it, any method could be implemented as static if you are willing to pass the object to it, this only seems to be a special case because the other parameter is also an instance.
因此,我们通过在点运算符的两个实例对象之间进行选择来略微冒犯我们对对称和抽象的搜索。但是如果你将 .method
看作。
然后是运算符,它实际上并不是真的一个问题。
Therefore, our search for symmetry and abstraction is slightly offended by having to choose between the two instance objects for the dot operator. But if you look at .method
as .
then operator, it isn't really a problem.
另外,进行功能样式链接的唯一方法是使用属性,即实例方法。你可能想要 thing.up.down.parent.next.distance(x)
才能工作。
Plus, the only way to do functional-style chaining is with an attribute, i.e., instance method. You probably want thing.up.down.parent.next.distance(x)
to work.
这篇关于Java:何时使方法静态v。实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!