gpt4 book ai didi

java - 这个类是不可变的吗?

转载 作者:行者123 更新时间:2023-11-29 09:32:39 25 4
gpt4 key购买 nike

<分区>

public final class Multi {
private final String name;
private final int number;
private final ArrayList<String> otherNames;

Multi(String name, int number) {
this.name = name;
this.number = number;
this.otherNames = new ArrayList<String>();
}

public void addOtherNames(String name) {
this.otherNames.add(name);
}


// All getter methods should be a copy of the class fields
public ArrayList<String> getOtherNames() {
return new ArrayList<String>(otherNames);
}
}

当涉及 ArrayList 等数据结构时,我无法理解不变性。

当我有诸如 addOtherNames 之类的方法时,它会在对象创建后将内容添加到列表中。

谢谢

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com