在重载构造函数中重用代码

发布于 2024-12-05 20:04:13 字数 728 浏览 1 评论 0原文

我的 BigBlock 类需要一些重载的构造函数。它们都需要以相同的方式初始化相同的几个字段。

执行此操作的正确方法是什么?是否要创建一个函数(例如下面示例中的Initialize)来执行这些操作,并让所有构造函数都调用该函数?

public class BigBlock {
    private Thing parentThing;
    Units lengthUnit;
    LabCoordinateSystem labCoordinateSystem;

    private void Initialize(){
        lengthUnit = parentThing.getPreferredUnits(0);
        labCoordinateSystem = parentThing.getCoordinateSystem();
    }

    BigBlock(Thing myThing){
        parentThing= myThing;
        Initialize();
    }

    BigBlock(Thing myThing, double x, double y, double z){
        parentThing= myThing;
        Initialize();
        // more code involving x, y, z
    }

    // a few more constructors

}

My BigBlock class needs a few overloaded constructors. All of them need to initialize the same few fields in the same way.

What is the proper way to do this? Is it to make a function, e.g. Initialize in the example below, that does these things, and have all constructors call that function?

public class BigBlock {
    private Thing parentThing;
    Units lengthUnit;
    LabCoordinateSystem labCoordinateSystem;

    private void Initialize(){
        lengthUnit = parentThing.getPreferredUnits(0);
        labCoordinateSystem = parentThing.getCoordinateSystem();
    }

    BigBlock(Thing myThing){
        parentThing= myThing;
        Initialize();
    }

    BigBlock(Thing myThing, double x, double y, double z){
        parentThing= myThing;
        Initialize();
        // more code involving x, y, z
    }

    // a few more constructors

}

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

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

发布评论

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

评论(4

装迷糊 2024-12-12 20:04:14

与往常一样,“哪种方式是……的正确/最佳方式”的答案是“取决于”。

如果 Initialize 背后的逻辑专门与对象创建相关,并且 Thing 在所有创建方面都很常见,那么您可以通过在最简单的构造函数上插入该行为并使用它存在于其他每一处。这会将初始化行为的使用集中在一个地方。

BigBlock(Thing myThing){
    parentThing= myThing;
    Initialize();
}

BigBlock(Thing myThing, double x, double y, double z){
    this(myThing);
    // more code involving x, y, z
}

在其他情况下,将 Initialize 作为单独的方法可能会很有用。例如,如果initialize执行一些可重用逻辑,就像您可以在与对象创建不同的时刻调用对象的“重置”。

As almost always, the answer to 'which is the proper/best way to...' is "depends".

If the logic behind Initialize is specifically coupled to object creation, and Thing is common in all creation aspects, you could model that by inserting that behavior on the simplest constructor, and using it in every other one. That would centralize this usage of the initialisation behavior in only one place.

BigBlock(Thing myThing){
    parentThing= myThing;
    Initialize();
}

BigBlock(Thing myThing, double x, double y, double z){
    this(myThing);
    // more code involving x, y, z
}

In other circumstances, it may be useful to have Initialize as a separate method. For instance, if initialize does some reusable logic, llike a "reset" on the object you may call in a moment different than object creation.

内心激荡 2024-12-12 20:04:13

通常,最好将所有构造函数链接到包含最多信息的单个构造函数,例如,

BigBlock(Thing myThing) {
    this(myThing, 0, 0, 0); // Assuming 0 is the default value for x, y and z
}

如果有不同的方法来调用构造函数,而不能有效地表示相同信息的子集,那么它会变得有点奇怪- 但那时我会说无论如何都有设计的味道。

请注意,当您在单个构造函数中获得所有实际逻辑时,您就不需要 Initialize 方法(应该是 initialize 来遵循 Java 命名约定) ,顺便说一句) - 这可能您可以将字段设为最终的,而以前您无法做到这一点。

Typically it's best to make all constructors chain to a single one which contains the most information, e.g.

BigBlock(Thing myThing) {
    this(myThing, 0, 0, 0); // Assuming 0 is the default value for x, y and z
}

It becomes slightly weirder if there are different ways to call the constructor which don't effectively represent subsets of the same information - but at that point I'd say there's a design smell anyway.

Note that by the time you've got all the real logic in a single constructor, you don't need your Initialize method (which should be initialize to follow Java naming conventions, btw) at all - which may also you can make fields final which previously you couldn't have done.

維他命╮ 2024-12-12 20:04:13

只需重用当前的构造函数即可。让所有其他构造函数调用初始化所有必需值的构造函数。

BigBlock(Thing myThing){
    parentThing = myThing;
    lengthUnit = parentThing.getPreferredUnits(0);
    labCoordinateSystem = parentThing.getCoordinateSystem();

}

BigBlock(Thing myThing, double x, double y, double z){
    this(myThing);
    // more code involving x, y, z
}

如果 xyz 需要成为初始化的一部分,BigBlock(Thing myThing) 应调用 < code>BigBlock(Thing myThing, double x, double y, double z) 使用默认值。

Just reuse your current constructor. Let every other constructor call the one that initialises all required values.

BigBlock(Thing myThing){
    parentThing = myThing;
    lengthUnit = parentThing.getPreferredUnits(0);
    labCoordinateSystem = parentThing.getCoordinateSystem();

}

BigBlock(Thing myThing, double x, double y, double z){
    this(myThing);
    // more code involving x, y, z
}

If x,y and z need to be part of the initalization, BigBlock(Thing myThing) should call BigBlock(Thing myThing, double x, double y, double z) with default values.

骑趴 2024-12-12 20:04:13

否 - 您可以使用此构造函数从构造函数调用其他构造函数。

BigBlock(Thing myThing) {
  this(myThing,0,0,0); // Pass default values for other constructors
}

No - you can call other constructors from a constructor using this.

BigBlock(Thing myThing) {
  this(myThing,0,0,0); // Pass default values for other constructors
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文