在什么情况下你会使用字段变量而不是局部变量?我发现在类中的2个或更多方法中使用变量时有点困难。我倾向于使用局部变量并将它们传递给另一个方法。
Under what circumstance would you use field variable instead of local variable? I found it a bit hard to decide when a variable is used in 2 or more methods in a class. I tend to use local variables and pass them to another method.
谢谢,
莎拉
推荐答案在面向对象的术语中,变量是否有意义作为对象的属性?如果是这样,你应该把它变成一个字段变量。如果没有,它可以采用任何一种方式。
In object-oriented terms, does the variable make sense as an attribute of the object? If so, you should make it a field variable. If not, it can go either way.
记住单一责任原则 - 精心设计的类应该只有1个责任,因此只有1个理由要改变。
Remember the Single Responsibility Principle -- well-designed classes should have only 1 responsibility, and thus only 1 reason to change.
更多推荐
何时使用字段变量?
发布评论