Java 中的构造函数重载 - 最佳实践 [英] Constructor overloading in Java - best practice

查看:26
本文介绍了Java 中的构造函数重载 - 最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有几个与此类似的主题,但我找不到一个有足够答案的主题.

There are a few topics similar to this, but I couldn't find one with a sufficient answer.

我想知道在 Java 中构造函数重载的最佳实践是什么.我已经对这个主题有了自己的想法,但我想听听更多建议.

I would like to know what is the best practice for constructor overloading in Java. I already have my own thoughts on the subject, but I'd like to hear more advice.

我指的是简单类中的构造函数重载和继承已经重载的类时的构造函数重载(意味着基类具有重载的构造函数).

I'm referring to both constructor overloading in a simple class and constructor overloading while inheriting an already overloaded class (meaning the base class has overloaded constructors).

谢谢:)

推荐答案

虽然没有官方指南",但我遵循 KISS 和 DRY 的原则.使重载的构造函数尽可能简单,最简单的方法是它们只调用 this(...).这样你只需要检查和处理一次参数.

While there are no "official guidelines" I follow the principle of KISS and DRY. Make the overloaded constructors as simple as possible, and the simplest way is that they only call this(...). That way you only need to check and handle the parameters once and only once.

public class Simple {

    public Simple() {
        this(null);
    }

    public Simple(Resource r) {
        this(r, null);
    }

    public Simple(Resource r1, Resource r2) {
        // Guard statements, initialize resources or throw exceptions if
        // the resources are wrong
        if (r1 == null) {
            r1 = new Resource();
        }
        if (r2 == null) {
            r2 = new Resource();
        }

        // do whatever with resources
    }

}

从单元测试的角度来看,测试类会变得很容易,因为您可以将资源放入其中.如果班级有很多资源(或一些面向对象的极客称之为合作者),请考虑以下两件事之一:

From a unit testing standpoint, it'll become easy to test the class since you can put in the resources into it. If the class has many resources (or collaborators as some OO-geeks call it), consider one of these two things:

public class SimpleParams {
    Resource r1;
    Resource r2;
    // Imagine there are setters and getters here but I'm too lazy 
    // to write it out. you can make it the parameter class 
    // "immutable" if you don't have setters and only set the 
    // resources through the SimpleParams constructor
}

Simple 中的构造函数只需要拆分 SimpleParams 参数:

The constructor in Simple only either needs to split the SimpleParams parameter:

public Simple(SimpleParams params) {
    this(params.getR1(), params.getR2());
}

...或者使 SimpleParams 成为一个属性:

…or make SimpleParams an attribute:

public Simple(Resource r1, Resource r2) {
    this(new SimpleParams(r1, r2));
}

public Simple(SimpleParams params) {
    this.params = params;
}

创建一个工厂类

制作一个为你初始化资源的工厂类,如果初始化资源有点困难,这是有利的:

Make a factory class

Make a factory class that initializes the resources for you, which is favorable if initializing the resources is a bit difficult:

public interface ResourceFactory {
    public Resource createR1();
    public Resource createR2();
}

然后以与参数类相同的方式完成构造函数:

The constructor is then done in the same manner as with the parameter class:

public Simple(ResourceFactory factory) {
    this(factory.createR1(), factory.createR2());
} 

两者结合

是的...您可以混合搭配两种方式,具体取决于当时对您来说更容易的方式.考虑到 Simple 类的使用方式相同,参数类和简单工厂类几乎是一回事.

Make a combination of both

Yeah... you can mix and match both ways depending on what is easier for you at the time. Parameter classes and simple factory classes are pretty much the same thing considering the Simple class that they're used the same way.

这篇关于Java 中的构造函数重载 - 最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆