何时使用字段变量? [英] When to use field variable?

查看:98
本文介绍了何时使用字段变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在什么情况下你会使用字段变量而不是局部变量?我发现在类中的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.

这篇关于何时使用字段变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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