Java 在构造函数中可重写的调用

发布于 2024-11-09 03:21:34 字数 466 浏览 0 评论 0原文

我知道从 Java 中的对象构造函数调用可重写方法是一种不好的(安全)做法。但是,例如,如果构造函数必须初始化一些数据,则调用相应的 setter 方法似乎是合理的,这样我就不会复制代码。设置者是公开的,不是最终的。是否有任何标准方法来处理这个问题,例如声明公共方法调用的私有设置方法?为了说明这一点,这里有一些代码:

class A {
    private double x,y;
    private privateSetX(double x1) { x=x1; }
    private privateSetY(double y1) { y=y1; }
    public A() { privateSetX(0); privateSetY(0); }
    public setX(double x1) { privateSetX(x1); }
    public setY(double y1) { privateSetY(y1); }
};

I know it is a bad (security) practice to call overridable methods from an object constructor in Java. However, for example, if the constructor has to initialize some data, it seems reasonable to call the respective setter method so that I don't copy code. The setters are public and not final. Is there any standard way of dealing with this, like declaring private setter methods, that the public ones call? To illustrate, here is some code:

class A {
    private double x,y;
    private privateSetX(double x1) { x=x1; }
    private privateSetY(double y1) { y=y1; }
    public A() { privateSetX(0); privateSetY(0); }
    public setX(double x1) { privateSetX(x1); }
    public setY(double y1) { privateSetY(y1); }
};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

独守阴晴ぅ圆缺 2024-11-16 03:21:34

如果您确实想要执行此操作,请创建一个由构造函数和公共 setter 都调用的辅助私有 setter 方法。

If you really want to do this, create a secondary private setter method that is called by both the constructor and the public setter.

无尽的现实 2024-11-16 03:21:34

我认为直接在构造函数中初始化数据成员是更好的做法。如果您调用一个方法,那么您必须查看该方法的实现以验证它是否确实在执行其看起来正在执行的操作。如果您直接分配给数据成员,您知道正在进行初始化。所以在你的代码中:

class A {
    private double x, y;
    public A() {
        x = 0;
        y = 0;
    }
    // ...
}

构造函数通常应该是简单的、确定性的并且明显正确的。直接分配可以满足这些目标。

I think that initialising the data members directly in the constructor is better practice. If you call a method, then you have to go look at that method implementation to verify that it really is doing what it looks like it's doing. If you assign to a data member directly, you know that the initialisation is taking place. So in your code:

class A {
    private double x, y;
    public A() {
        x = 0;
        y = 0;
    }
    // ...
}

A constructor should usually be simple, deterministic, and obviously correct. Direct assignment satisfies these goals.

最佳男配角 2024-11-16 03:21:34

创建在构造过程中需要设置许多不同字段的对象的更好方法是使用构建器模式

我不会重复其他人的努力,而是会向您指出一个最优秀的SO答案 关于这个话题。

如果问题是您需要在构造函数期间重写 setter,您可以创建一个 Builders 层次结构,而不是您要构建的主类的层次结构,或者除了您要构建的主类的层次结构之外。

A better way to create an object that needs to have lots of different fields set during construction is to use the Builder Pattern.

Rather than duplicate the efforts of others, I will just point you to a most excellent SO answer on this topic.

If the problem is that you need to override setters during the constructor, you can create a hierarchy of Builders instead of, or in addition to, the hierarchy of the primary class that you're trying to build.

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