有没有一个更简单的术语可以代表“非破坏性 setter”?

huangapple go评论85阅读模式
英文:

Is there a simpler term that stands for "non-destructive setter"?

问题

在这样的代码中,setValue 方法通常一般被称为“setter”,但是像 withValue 方法这样的(即返回其字段被更改的新对象)一般被称为什么?

class MyClass {
   private int value;
   public MyClass(int value) {this.value=value;}
   // 一个获取器
   public int getValue() {return value;}
   // 一个(破坏性的)setter
   public void setValue(int value) {this.value=value;}
   // 一个(非破坏性的)setter <- 是否有比这更简单的术语?
   public MyClass withValue(int value) {return new MyClass(value);}
}
英文:

In such a code, setValue method is usually called as "setter" in general, but how withValue method, (i.e. returns new object whose fields(s) is/are changed), is called in general?

class MyClass {
   private int value;
   public MyClass(int value) {this.value=value;}
   // a getter
   public int getValue() {return value;}
   // a (destructive) setter
   public void setValue(int value) {this.value=value;}
   // a (non-destructive) setter &lt;- Is there a simpler term than this? 
   public MyClass withValue(int value) {return new MyClass(value);}
}  

答案1

得分: 1

通常这些方法的前缀是 with

MyClass alternateVersion = myClass.withValue(5);
英文:

The usual prefix for those methods is with:

MyClass alternateVersion = myClass.withValue(5);

huangapple
  • 本文由 发表于 2020年9月30日 12:26:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/64130857.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定