Scala getter/setter - 最佳实践?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26012039/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 06:35:40  来源:igfitidea点击:

Scala getters/setters - best practice?

javascalanaming-conventions

提问by xwinus

I'm Java SE/EE developer, but beginner in Scala. In Java, when I have some private fields which should be accessible to other code, I use getX()/setX()classic style of getters/setters. However, not sure how about Scala. I noticed that in Scala, naming convention of getter/setter for field is to use the same name as the field's one. So is it OK to simply set the field public, or should I use this style of getter/setter?:

我是 Java SE/EE 开发人员,但 Scala 初学者。在 Java 中,当我有一些其他代码应该可以访问的私有字段时,我使用getX()/setX()经典风格的 getter/setter。但是,不确定 Scala 怎么样。我注意到在 Scala 中,字段的 getter/setter 命名约定是使用与字段名称相同的名称。那么可以简单地设置 field public,还是应该使用这种样式的 getter/setter ?:

private var _value = .....
def value = _value
def value_= (newVal:Int) = _value = newVal

Is it OK (according to scala naming convention) to put underscore before the field name itself?

在字段名称本身之前放置下划线是否可以(根据 Scala 命名约定)?

Thank you.

谢谢你。

回答by Chris K

The Scala Style Guidecovers this quite nicely.

斯卡拉风格指南涵盖了这相当不错。

For accessors of properties, the name of the method should be the name of the property.

对于属性的访问器,方法的名称应该是属性的名称。

Scala does not follow the Java convention. Scala promotes the view that a caller should not be able to tell the difference between a field access and a method call, which means that the convention is to give them both the same name; thus reducing the amount of code change required should a field be changed to a method or visa versa.

Scala 不遵循 Java 约定。Scala 提倡这样一种观点,即调用者不应该能够区分字段访问和方法调用之间的区别,这意味着约定是为它们提供相同的名称;因此,如果将字段更改为方法或反之,则减少所需的代码更改量。

Is it OK (according to scala naming convention) to put underscore before the field name itself?

在字段名称本身之前放置下划线是否可以(根据 Scala 命名约定)?

Scala convention is to prefix fields that we want to be private that otherwise have the same name as a public method, or to postfix it with a zero. Either approach is acceptable.

Scala 约定是为我们希望私有的字段添加前缀,否则它们与公共方法具有相同的名称,或者用零作为后缀。任何一种方法都是可以接受的。

private var _value = .....
def value = _value
def value_= (newVal:Int) = _value = newVal

However, given this example the extra lines are not necessary. The convention exists so that we can use this shorter version and then change it later to the more explicit version when/if we need to without having to make changes at every call site.

但是,鉴于此示例,不需要额外的行。约定存在以便我们可以使用这个较短的版本,然后在以后/如果需要时将其更改为更明确的版本,而不必在每个调用站点进行更改。

var value:Int = 0

回答by Konstantin Yovkov

According to the Scala docs:

根据Scala 文档

Scala does not follow the Java convention of prepending set/get to mutator and accessor methods (respectively). Instead, the following conventions are used:

Scala 不遵循 Java 约定,即在 mutator 和 accessor 方法(分别)之前添加 set/get。相反,使用以下约定:

  • For accessors of properties, the name of the method should be the name of the property.
  • In some instances, it is acceptable to prepend “is” on a boolean accessor (e.g. isEmpty). This should only be the case when no corresponding mutator is provided.
  • For mutators, the name of the method should be the name of the property with “_=” appended.
  • 对于属性的访问器,方法的名称应该是属性的名称。
  • 在某些情况下,可以is在布尔访问器(例如 isEmpty)上添加“ ”。只有在没有提供相应的修改器时才会出现这种情况。
  • 对于 mutators,方法的名称应该是附加了“_=”的属性名称。

Example:

例子:

class Foo {
  def bar = ...
  def bar_=(bar: Bar) {
    ...
  }
  def isBaz = ...
}