使用类变量或传递参数更好吗? [英] Is it better to use class variables or pass parameters?
问题描述
这个一直困扰着我。打一个自我更好吗?在
前面的任何变量将由多个类方法使用
或者我应该在方法之间传递变量吗?
Flamewar ........现在!
jk,我真的想要其他意见。
谢谢,
Derek
This one has always bugged me. Is it better to just slap a "self" in
front of any variable that will be used by more than one class method
or should I pass around variable between the methods?
Flamewar........NOW!
jk, I really do want other opinions.
Thanks,
Derek
推荐答案
2006年3月1日11:32:02 -0800,Derek Basch写道:
On 1 Mar 2006 11:32:02 -0800, Derek Basch wrote:
这个一直困扰着我。打一个自我更好吗?在
前面的任何变量将由多个类方法使用
或者我应该在方法之间传递变量吗?
This one has always bugged me. Is it better to just slap a "self" in
front of any variable that will be used by more than one class method
or should I pass around variable between the methods?
我认为没有明确的一般回答。一个标准可能是
实例变量应该是定义对象状态的那些,
定义它的含义。
关于其他对我来说这是品味问题。
-
USB牧师只有10个
I think there is no clear "general" answer. A criterion could be that
instance variables should be those who define the state of the object, that
define its "meaning".
About the others to me it''s a matter of taste.
--
USB Priests for only 10
2006年3月1日星期三,Derek Basch写道:
On Wed, 1 Mar 2006, Derek Basch wrote:
这个一直在窃听我。打一个自我更好吗?在
前面的任何变量将由多个类方法使用
或者我应该在方法之间传递变量吗?
This one has always bugged me. Is it better to just slap a "self" in
front of any variable that will be used by more than one class method
or should I pass around variable between the methods?
As一般来说,我只会将参数转换为实例变量,而
否则它将成为所有(或几乎所有)方法的参数。虽然这可能是一个过于机械的视角,但我想将实例变量视为每个方法的隐含参数。它帮助我
控制范围并鼓励更好的凝聚力。
-
。:[dave benjamin - (拉面) / sp00) - http://spoomusic.com/ ] :.
一个人的常数是另一个人的变量。 - alan perlis
As a general rule, I only turn a parameter into an instance variable when
it would otherwise be a parameter of all (or nearly all) methods. Though
this is perhaps an overly mechanical perspective, I like to think of
instance variables as implicit parameters to every method. It helps me
keep scope under control and encourages better cohesion.
--
.:[ dave benjamin -( ramen/sp00 )- http://spoomusic.com/ ]:.
"one man''s constant is another man''s variable" - alan perlis
这篇关于使用类变量或传递参数更好吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!