简体   繁体   English

将实例变量传递给实例方法而不是直接访问它们?

[英]Passing instance variables to instance methods vs. directly accessing them?

Imagine I have a class with an instance member 想象一下,我有一个带有实例成员的类

String instanceMember;

Further I have like 4 instance methods accessing this member. 此外,我有4个实例方法访问此成员。 Now I am wondering if there is any real pro/con for either directly accessing the instance member from the instance methods or to pass the instance member as a parameter to each of the instance methods? 现在我想知道是否有任何真正的pro / con从实例方法直接访问实例成员或将实例成员作为参数传递给每个实例方法?

Passing the value as a parameter would imply that you are going to perform the calculation with the parameter value rather than the encapsulated value. 将值作为参数传递将意味着您将使用参数值而不是封装值执行计算。

If you are going to operate on encapsulated data then there is no reason to have it as a parameter. 如果您要对封装数据进行操作,则没有理由将其作为参数。

The reason for having instance variables in the first place is to avoid passing them as parameters: your instance method gets to access them "for free", because they are already in the method's scope. 首先使用实例变量的原因是为了避免将它们作为参数传递:您的实例方法可以“免费”访问它们,因为它们已经在方法的范围内。

There are two reasons to pass instance variables to methods, though: 但是,将实例变量传递给方法有两个原因:

  • The method is static - class methods cannot access instance variables, so you need to pass them explicitly, or pass the object on which to operate 该方法是static - 类方法无法访问实例变量,因此您需要显式传递它们,或者传递要操作的对象
  • You need to use pass-by-value semantic - in other words, you would like to modify the value passed in, and you would like to avoid creating a local variable. 您需要使用按值传递语义 - 换句话说,您希望修改传入的值,并且您希望避免创建局部变量。 This is often the case when the method is recursive . 当方法是递归时,通常就是这种情况。

If you find yourself writing recursive code that modifies a parameter which started off as an instance variable, in many cases it may be a good idea to make your method private static , and add a public instance method to start the recursive chain and to harvest the results. 如果您发现自己编写的递归代码修改了一个以实例变量开头的参数,在许多情况下,将您的方法设为private static是一个好主意,并添加一个public实例方法来启动递归链并收集结果。

If these methods are public methods that manipulate the state of a persistent member variable, you shouldn't really be passing it around. 如果这些方法是操纵持久成员变量状态的公共方法,那么你不应该真正传递它。 For example, if you have to do something like the following, it should probably be manipulated directly by the method instead of being passed in: 例如,如果您必须执行以下操作,则可能应该由方法直接操作而不是传入:

myObject.SomeMethod(myObject.instanceMember, 15);

This should really just be: 这应该只是:

myObject.SomeMethod(15);

If it's something that might change per call (such as, for example, the mysterious 15 in the method above), you'll want to pass it around. 如果它是每次调用可能改变的东西(例如,上面方法中的神秘15 ),你会想要传递它。

It will depend a lot on how you use it. 这将取决于你如何使用它。 If the usage of you class is infrequent, then there will be no obvious difference. 如果你的课程用量很少,那么就没有明显的区别。

If you are using the class instance a lot, and by many threads, then in some conditions, passing it as a parameter would work a bit faster. 如果你经常使用类实例,并且通过许多线程,那么在某些情况下,将它作为参数传递会更快一些。

Usually instance members are accessed directly by class members with exeptions of static functions (due to obvious reasons). 通常,实例成员由具有静态函数扩展的类成员直接访问(由于显而易见的原因)。

So follow your coding conventions and don't worry. 因此,请遵循您的编码约定,不要担心。 By the time it would really matter you will know the answer by heart. 到了真正重要的时候,你会心里明白答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM