何时使用对象实例变量与将参数传递给方法 [英] When to use an object instance variable versus passing an argument to the method

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

问题描述

如何在将参数传递给方法与简单地将其声明为对所有对象方法可见的对象实例变量之间做出决定?

How do you decide between passing arguments to a method versus simply declaring them as object instance variables that are visible to all of the object's methods?

我更喜欢将实例变量保存在列表中在课程结束时,但随着我的程序的增长,此列表会越来越长。我想知道是否足够频繁地传递变量,它应该只对需要它的所有方法可见,但是我想知道,如果一切都是公开的,就根本不需要传递任何东西!

I prefer keeping instance variables in a list at the end of the Class, but this list gets longer as my program grows. I figure if a variable is passed often enough it should just be visible to all methods that need it, but then I wonder, "if everything is public there will be no need for 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天全站免登陆