何时传递参数以及何时使用实例变量 [英] when to pass a parameter and when to use an instance variable

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

问题描述

你们如何决定是在本地跟踪某项内容,然后将其传递给您在其上调用的每个方法,还是声明一个实例变量并在方法中使用它?

How do you guys decide between keeping track of something locally and then just passing it in to every method you call on it, or declaring an instance variable and using it in the methods?

我倾向于选择在类末尾的列表中保留的实例变量.但是随着我的程序变得越来越复杂,这个列表越来越长……我认为,如果某件事通过得足够多,那么所有需要它的男孩和女孩都应该可以看到它,但是我开始怀疑, 为什么不仅仅公开一切!那么就根本不需要传递任何东西了!"

I tend to prefer instance variables kept in a list at the end of the Class. But as my programs become more and more complicated, this list gets longer and longer... I figure that if something is getting passed often enough it should just be visible to all the boys and girls who need it, but then I start wondering, "why not just make everything public! Then there will be no more need of passing anything at all!"

推荐答案

由于您要引用实例变量,因此我假设您使用的是面向对象的语言.在某种程度上,何时使用实例变量,如何定义它们的范围以及何时使用局部变量是主观的,但是在创建类时可以遵循一些经验法则.

Since you're referring to instance variables, I'm assuming that you're working in an object-oriented language. To some degree, when to use instance variables, how to define their scope, and when to use local variables is subjective, but there are a couple of rules of thumb you can follow whenever creating your classes.

  • 实例变量通常被视为类的属性.可以将它们视为将要从您的类中创建的对象的形容词.如果您的实例数据可用于帮助描述对象,那么可以肯定地说,它是实例数据的不错选择.

  • Instance variables are typically considered to be attributes of a class. Think of these as adjectives of the object that will be created from your class. If your instance data can be used to help describe the object, then it's probably safe to bet it's a good choice for instance data.

在方法范围内使用局部变量来帮助他们完成工作.通常,方法应具有获取某些数据,返回某些数据和/或进行处理的目的. /在某些数据上运行算法.有时,将局部变量视为帮助方法自始至终的方法会有所帮助.

Local variables are used within the scope of methods to help them complete their work. Usually, a method should have a purpose of getting some data, returning some data, and/or proccessing/running an algorithm on some data. Sometimes, it helps to think of local variables as ways of helping a method get from beginning to end.

实例变量范围不仅用于安全性,而且还用于封装.不要以为目标应该是使所有变量保持私有".在继承的情况下,将变量设置为受保护通常是一个很好的选择.无需将所有实例数据标记为公共,而是为需要访问外界的对象创建吸气剂/吸气剂.不要让它们全部可用-只是您需要的那些.这将贯穿于整个开发生命周期-一开始就很难猜测.

Instance variable scope is not just for security, but for encapsulation, as well. Don't assume that the "goal should be to keep all variables private." In cases of inheritance, making variables as protected is usually a good alternative. Rather than marking all instance data public, you create getters/setters for those that need to be accessed to the outside world. Don't make them all available - only the ones you need. This will come throughout the development lifecycle - it's hard to guess from the get go.

在围绕类传递数据时,如果不看一些代码就很难说您正在做的事情是一种好习惯.有时,直接对实例数据进行操作是可以的.其他时间不是.我认为,这是经验带来的-随着面向对象的思维能力的提高,您会发展出一些直觉.

When it comes to passing data around a class, it's difficult to say what you're doing is good practice without seeing some code . Sometimes, operating directly on the instance data is fine; other times, it's not. In my opinion, this is something that comes with experience - you'll develop some intuition as your object-oriented thinking skills improve.

这篇关于何时传递参数以及何时使用实例变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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