英文:
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 <- 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);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论