重用带有最终实例字段的构造函数
假设我有以下试图保持不可变的类
public class Computation {
private final Operation operation;
private final double epsilon;
public Computation(Operation operation) {
this.operation = operation;
//Default value
epsilon = 0.01;
}
public Computation(Operation operation double epsilon) {
this(operation);
//Won't compile as epsilon is final and is set by the other constructor
this.epsilon = epsilon;
}
}
,并且为了解决这个问题,我们假设我不想为此类使用构建器(这将解决问题)。
所以问题是:
有没有办法在不删除 epsilon 的final修饰符并保留两个构造函数的情况下实现这种行为?
也就是说,不做类似的事情
public class Computation {
private final Operation operation;
private final double epsilon;
public Computation(Operation operation Double epsilon) {
this(operation);
this.epsilon = (epsilon == null) ? 0.01 : epsilon;
}
}
,也不使用构建器
public class Computation {
private final Operation operation;
private final double epsilon;
private Computation(Builder builder) {
this.operation = builder.operation;
this.epsilon = builder.epsilon;
}
public static class Builder {
private final Operation operation;
//Default value
private double epsilon = 0.01;
public Builder(Operation operation) {
this.operation = operation;
}
public Builder epsilon(double epsilon) {
this.epsilon = epsilon;
return this;
}
public Computation build() {
return new Computation(this);
}
}
Let's suppose that I have the following class which tries to be immutable
public class Computation {
private final Operation operation;
private final double epsilon;
public Computation(Operation operation) {
this.operation = operation;
//Default value
epsilon = 0.01;
}
public Computation(Operation operation double epsilon) {
this(operation);
//Won't compile as epsilon is final and is set by the other constructor
this.epsilon = epsilon;
}
}
And, for the sake of the question, let's assume that I do not want to use a builder for this class (which would solve the issue).
So the question is:
Is there a way to achieve this behaviour without removing the final modifier to epsilon and keeping the two constructors?
That is, without doing something like
public class Computation {
private final Operation operation;
private final double epsilon;
public Computation(Operation operation Double epsilon) {
this(operation);
this.epsilon = (epsilon == null) ? 0.01 : epsilon;
}
}
and without using a builder
public class Computation {
private final Operation operation;
private final double epsilon;
private Computation(Builder builder) {
this.operation = builder.operation;
this.epsilon = builder.epsilon;
}
public static class Builder {
private final Operation operation;
//Default value
private double epsilon = 0.01;
public Builder(Operation operation) {
this.operation = operation;
}
public Builder epsilon(double epsilon) {
this.epsilon = epsilon;
return this;
}
public Computation build() {
return new Computation(this);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的 - 反转逻辑,以便具有更少参数的构造函数调用具有更多参数的构造函数:
基本上,这样您就可以得到相当多的构造函数,它们都只委托给一个构造函数“真正的”构造函数完成所有实际工作。
Yes - reverse the logic so that your constructor with fewer parameters calls the one with more:
Basically that way you can end up with quite a few constructors which all just delegate to one "real" constructor which does all the actual work.