重写 toString 时是使用 getter 方法更好还是直接访问私有字段更好?
我见过这两种方法的使用,但从未听说过出于任何特定原因其中一种方法优于另一种方法。
public String toString() {
return this.field1 + " " + this.field2;
}
与
public String toString() {
return getField1() + " " + getField2();
}
我在示例中使用字符串连接来保持代码简洁相比。
I've seen both approaches used but have never heard that one way is preferred over the other for any particular reason.
public String toString() {
return this.field1 + " " + this.field2;
}
versus
public String toString() {
return getField1() + " " + getField2();
}
I used String concatenation in my example to keep the code brief.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
使用吸气剂(如果有的话)!
也许有一天,您更改了代码,以便 getter 不仅会返回字段值,还会执行更多操作或以不同的方式创建结果。然后你会更加高兴你一直使用吸气剂。
但像往常一样,我的建议也有例外 - 如果您允许覆盖 getter 方法,您对 toString() 方法有何期望,您希望它使用类字段还是 - 可能覆盖 - getter 方法的结果。
因此,像往常一样,这取决于情况,但我会使用 getter,除非我有充分的理由直接访问字段。
Use getters, if you have them!
Maybe, one day you change the code so that a getter will not only return the fields value but do something more or create the result in a different way. Then you'll be more then happy that you used getters consistently.
But as usual, there are excemptions from my advice - what do you expect from the toString() method if you allow overriding of the getter methods, do you want it use the classes fields or the result of the - maybe override - getter method.
So, as usual, it depends, but I'd use getters unless I have a good reason to access the fields directly.
如果访问器方法除了返回字段值之外还提供任何附加逻辑(例如大写字符串或类似的内容),则可能会首选使用访问器方法。如果您的类被设计为扩展,我会倾向于访问器,因为它们可能会被覆盖。
Using accessor methods might be preferred if they provide any additional logic on top of just returning the field's value (like uppercasing a string, or something like that). If your class is designed to be extended, I would tend towards accessors because they may be overridden.
在 toString 中使用 getter 是一种矫枉过正的行为。并且您不应该将 toString 用于非调试目的。
Using getters in toString is an overkill. And you should not be using toString for non debug purposes.
我更喜欢直接访问私有变量的方法。恕我直言,吸气剂方法会增加混乱。
I would prefer to the approach to access the private variables directly. IMHO the gettter methods increase the clutter.
您有什么理由使用 getter 方法吗?他们做了什么有趣的事吗?
...或者您只是使用它们来公开内部数据,而实际上没有将字段本身公开(穷人的封装)?
如果是前者,那么当然也可以在这里使用它们!如果是后者,那么没关系 - 您可能无论如何都想使用它们,以防万一您给它们一个真正的目的,但否则不会有任何区别。
Do you have any reason to use getter methods? Do they do anything interesting?
...Or are you just using them to expose internal data without actually making the fields themselves public (poor man's encapsulation)?
If the former, then yes by all means use them here as well! If the latter, then it doesn't matter - you might want to use them anyway, just in case you ever give them a real purpose, but it won't make one bit of difference otherwise.
使用
String.format("%s %s", this.getField1(), this.getField2());
是最好的方法,因为.getXXX()
方法中可能包含您想要的逻辑,例如将 NULL 引用转换为空字符串或一些您通过盲目访问私有实例无法获得的默认值。这是最安全的方法,以防万一有人向.getXXX()
方法添加逻辑,并且他们不考虑更新.toString() 实现。
using
String.format("%s %s", this.getField1(), this.getField2());
would be the best way to do this, becuase the.getXXX()
methods might have logic in them that you want like converting NULL references into empty strings or some default value that you would not get by just blindly accessing the private instances. It is the safest way to do it, just in case someone comes along and adds logic to a.getXXX()
method and they don't think to update the.toString()
implemenation.如果您的类具有字段的 getter 和 setter,则无论是使用 getter 和 setter 还是直接访问字段,您都应该在该类的整个实现中保持一致。混合抽象级别通常没有意义。否则,你怎么能合理地在某些地方使用它们而在其他地方不使用它们呢?此外,如果 getter 的实现方式发生变化,您最终可能会得到微妙的包。 toString() 是实现的一部分,因此它也应该保持一致。
就性能而言,这并不重要 - 无论如何,您的琐碎 getter 和 setter 都应该是最终方法,因此无论如何它们都应该是内联的。如果它们不是最终的,您应该问自己为什么,并且可能避免直接访问这些字段,除非这确实是您想要做的。
If your class has getters and setters for fields, you should be consistent throughout the implementation of that class in whether you use getters and setters or access the fields directly. It doesn't generally make sense to mix levels of abstraction. Otherwise, how can you rationalize using them in some places but not in others? In addition, if there is ever a change in how the getters are implemented, you could end up with subtle bags. toString() is part of the implementation, so it should be consistent as well.
In terms of performance, it shouldn't matter - your trivial getters and setters should be final methods anyway, so they should be inlined anyway. IF they're not final, you should ask yourself why and probably avoid accessing the fields directly unless this is really what you mean to do.