何时使用字段变量?

编程入门 行业动态 更新时间:2024-10-14 10:45:36
本文介绍了何时使用字段变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

更多推荐

何时使用字段变量?

本文发布于:2023-11-27 13:10:40,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1638210.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字段   变量

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!