简体   繁体   English

治疗要在班内进行。 通过getter还是显式的?

[英]Treatment to field inside the class. Through getter or explicit?

consider the class: 考虑类:

class MyClass{
   MyOtherClass obj;
   //setObj and getObj methods
   public void someMethod(){
      ... 
      //access to obj needs.
      ...    
   }
}

How to right replace 如何正确更换

//access to obj needs. //访问obj需求。

through getter or explicitly? 通过吸气剂还是显式地?

PS 聚苯乙烯

I saw both variants in my expirience. 我从经验中看到了这两种变体。

Personally I would say it depends on the level of "connection" between both classes. 我个人认为这取决于两个类之间的“连接”级别。 If they are in the same package and part of the same "mecanism" (one would have no reason to exist without the other), bypassing accessors is acceptable. 如果它们位于同一程序包中且属于同一“机制”的一部分(一个没有另一个就没有理由),则绕过访问器是可以接受的。

So here we're talking about code in Class MyClass accessing information in an instance of MyOtherClass . 因此,这里我们讨论的是MyClass类中的代码,该类在MyOtherClass实例中访问信息。

Typically you don't get a choice. 通常情况下,您别无选择。 If MyOtherClass exposes a getter for a data member, it's unlikely to also expose that data member. 如果MyOtherClass公开数据成员的吸气剂,则不太可能公开该数据成员。 If it does (even if the data member is, say, protected but the accessor is public ), the design is a bit questionable. 如果确实如此(即使数据成员protected但访问器是public ),那么设计就会有些问题。

But if you do have the choice, I would use the getter, rather than the exposed data member. 但是,如果您有选择的话,我将使用getter而不是公开的数据成员。 It's a bit subjective, but using data members rather than accessors more tightly binds the classes together. 有点主观,但是使用数据成员而不是访问器会更紧密地将类绑定在一起。 In my protected/public example, you'd have more work to do if for any reason you wanted to move MyClass to a different package. 在我的protected/public示例中,如果出于任何原因想要将MyClass移至其他程序包,您将需要做更多的工作。


It's worth noting that using the getter is not more expensive in performance terms with a decent JVM (such as the one from Sun). 值得注意的是,使用性能良好的getter并不需要使用像样的JVM(例如Sun的JVM)。 If the code becomes a performance "hotspot" for whatever reason (or possibly even if it doesn't), the JVM's JIT will convert the call to the getter into a direct access anyway (presuming it's a pure getter), so you get the benefit of abstraction at the coding/design-time without the function call overhead at runtime. 如果代码由于某种原因(甚至可能不是)成为性能的“热点”,那么JVM的JIT仍会将对getter的调用转换为直接访问(假设它是纯getter),因此您得到了在编码/设计时抽象的好处,而在运行时没有函数调用开销。

To answer this, let's first see why getters and setters were introduced in the first place. 为了回答这个问题,让我们首先来看一下为什么首先要使用getter和setter方法。 It is clear that direct access to data members is simpler. 显然,直接访问数据成员更为简单。

SOme of the reasons are: SOme的原因是:

  • for a better encapsulation, to hide the property implementation from a class user. 为了更好的封装,向类用户隐藏属性实现。 For example you can internally store a temperature value in C and return it by a getter in F. 例如,您可以在内部将温度值存储在C中,然后通过吸气剂将其返回到F中。
  • for more control over the access. 以获得对访问的更多控制。 If you want to do something more besides pure getting/setting a piece of data, you would need a method. 如果除了纯粹获取/设置一条数据以外,您还想做其他事情,则需要一种方法。 For example, you might want to log the change of value for audit purpose 例如,您可能想要记录值的更改以进行审核
  • methods are much more "interface friendly" than pure data members. 方法比纯数据成员更“界面友好”。

In this case the class itself accesses its own property. 在这种情况下,类本身将访问其自己的属性。 Are you sure you want that? 您确定要吗?

If so, let's see the reasons: 如果是这样,让我们​​看看原因:

Encapsulation is definitelly not needed, since the class itself accesses its own attributes. 绝对不需要封装,因为类本身访问其自己的属性。

Do you need to somehow control access here? 您是否需要以某种方式控制此处的访问? Do you need to do something else, besides get/set? 除了获取/设置,您还需要做其他事情吗? Are there any other possible users of this class? 此类是否还有其他可能的用户?

If all these answers are NO, ans especially if the only user of this class the mentioned method, then go for a simpler option and use direct access, without getters/setters. 如果所有这些答案都为“否”,则回答(特别是如果此类的唯一用户使用提到的方法),然后选择一个更简单的选项并使用直接访问方式,而无需使用getter / setter方法。

If some of the answers is true, just make a simple trade-off and decide. 如果某些答案是正确的,只需进行简单的权衡并做出决定。

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

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