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

查看:185
本文介绍了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的原则。使重载的构造函数尽可能简单,最简单的方法是他们只调用这个(...)。这样你只需要检查和处理参数一次,只需要一次。

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
    }

}

从单元测试的角度来看,测试类将变得很容易,因为你可以把资源放入其中。如果类有许多资源(或者一些OO-geek调用它的协作者),请考虑以下两种情况之一:

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());
}

...或make 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();
}

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

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天全站免登陆