In object-oriented and functional programming, an immutable object is an object whose state cannot be modified after it is created. This is in contrast to a mutable object, which can be modified after it is created.
下面是可变类还是不可变类?我认为它是可变的,因为静态字段 singleton
在 new MavenPush(project)
之后发生了变化,但我需要 100% 正确的答案。这是 Groovy 代码,但它就像 Java 代码
final class MavenPush {
private static volatile MavenPush singleton = null
private final def project
private MavenPush(project) {
this.project = project
}
static MavenPush with(project) {
if (singleton == null) {
synchronized (MavenPush.class) {
if (singleton == null) {
singleton = new MavenPush(project)
}
}
}
return singleton
}
}
最佳答案
类的实例是不可变的,当类的字段发生变化时,类是可变的。
关于oop - 下面是可变类还是不可变类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51651388/