重用带有最终实例字段的构造函数

发布于 2024-08-22 12:54:17 字数 1656 浏览 5 评论 0原文

假设我有以下试图保持不可变的类

    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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寒冷纷飞旳雪 2024-08-29 12:54:17

是的 - 反转逻辑,以便具有更少参数的构造函数调用具有更多参数的构造函数:

public Computation(Operation operation) {
    this(operation, 0.01);
}

public Computation(Operation operation, double epsilon) {
    this.operation = operation;
    this.epsilon = epsilon;
}

基本上,这样您就可以得到相当多的构造函数,它们都只委托给一个构造函数“真正的”构造函数完成所有实际工作。

Yes - reverse the logic so that your constructor with fewer parameters calls the one with more:

public Computation(Operation operation) {
    this(operation, 0.01);
}

public Computation(Operation operation, double epsilon) {
    this.operation = operation;
    this.epsilon = epsilon;
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文