简体   繁体   English

调用类的参数化/默认构造函数?

[英]Invoking parameterized/default constructor of a class?

I have a class with two constructors. 我有一个有两个构造函数的类。

Class Sample{

  private ClassOne classOne;
  private ClassTwo classTwo;
  private ClassThree classThree;


  public Sample(){
    classOne = new ClassOne();
    classTwo = new ClassTwo();
    classThree = new ClassThree();
  }

  public Sample(int id){

    classOne = new ClassOne(id);
    classTwo = new ClassTwo(id);
    classThree = new ClassThree(id);

  }

  //some code here
}

have a class Sample with two constructors. 有一个带有两个构造函数的Sample。 Need to instantiate three more classes in Sample class as mentioned(ClassOne, ClassTwo and ClassThree). 需要在Sample类中实例化另外三个类(ClassOne,ClassTwo和ClassThree)。 All three classes contains default and parameterized constructors. 所有三个类都包含默认和参数化构造函数。 If Sample class's default constructor is invoked then ClassOne, ClassTwo and ClassThree 's default cosntructor should get invoked. 如果调用了Sample类的默认构造函数,则应调用ClassOne,ClassTwo和ClassThree的默认cosntructor。 If Samples parameterized constructor is invoked then ClassOne,ClassTwo and ClassThree 's parameterized constructor is invoked. 如果调用了Samples参数化构造函数,则调用ClassOne,ClassTwo和ClassThree的参数化构造函数。

I wrote above the code. 我在上面写了代码。 is there any elegant way to do that? 有没有优雅的方法呢?

Assuming you have no other use for id inside your Sample class you could use the factory method pattern: 假设您在Sample类中没有其他id用途,您可以使用工厂方法模式:

class Sample {
    private ClassOne classOne;
    private ClassTwo classTwo;
    private ClassThree classThree;

    Sample(ClassOne classOne, ClassTwo classTwo, ClassThree classThree) {
        this.classOne = classOne;
        this.classTwo = classTwo;
        this.classThree = classThree;
    }

    public static Sample factory(int id) {
        ClassOne classOne = null;
        ClassTwo classTwo = null;
        ClassThree classThree = null;
        if ( id == -1 ) {
            classOne = new ClassOne();
            classTwo = new ClassTwo();
            classThree = new ClassThree();
        }
        else {
            classOne = new ClassOne(id);
            classTwo = new ClassTwo(id);
            classThree = new ClassThree(id);
        }
        return new Sample(classOne, classTwo, classThree);
    }
}

Your specific example is probably cleaner in its original formulation, but this is a good way to move complex construction logic out of constructors. 您的具体示例可能在其原始配方中更清晰,但这是将复杂构造逻辑移出构造函数的好方法。

You might change your Sample constructor to accept an Integer object argument. 您可以更改Sample构造函数以接受Integer对象参数。 Then the default constructor for Sample could just invoke the Integer construct passing null. 然后,Sample的默认构造函数可以调用传递null的Integer构造。 You could take it a step further and change the constructors of ClassOne, ClassTwo, and ClassThree to also accept an Integer object argument and handle the null case elegantly: 您可以更进一步,更改ClassOne,ClassTwo和ClassThree的构造函数,以接受Integer对象参数并优雅地处理null case:

class Sample {
  private ClassOne classOne;
  private ClassTwo classTwo;
  private ClassThree classThree;

  public Sample(){
    this(null);
  }

  public Sample(Integer id){
    super();
    classOne = new ClassOne(id);
    classTwo = new ClassTwo(id);
    classThree = new ClassThree(id);
  }
  ...
}

class ClassOne {
  private int id;

  public ClassOne(Integer id) {
    super();
    if (id == null) {
      this.id = 0; // or whatever default value
    } else {
      this.id = id.intValue();
    }
  }
  ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从另一个类中的public类型的参数化构造函数中调用默认类型的参数化构造函数? - How can I call parameterized constructor of default type from a parameterized constructor of type public which is in another class? 调用默认构造函数的含义 - meaning of invoking default constructor 如何从默认构造函数调用参数化构造函数? - how to call parameterized constructor from default constructor? 我可以在java中的公共类中调用参数化构造函数中的默认构造函数吗? - Can I call default constructor from parameterized constructor inside public class in java? 如何使用默认访问修饰符创建构造函数是参数化构造函数的类的对象 - How to create the object of class where constructor is parameterized constructor using default access modifier 用参数化构造函数模拟最终类 - Mocking final class with parameterized constructor 将参数化的Class实例传递给构造函数 - Passing parameterized Class instance to the constructor 在构造函数中带有2个参数的参数化运行器类 - Parameterized runner class with 2 arguments in constructor 是否需要调用将变量初始化为默认值的构造函数? - Is invoking a constructor needed for initializing variables to default values? 当明确需要默认构造函数以及参数化构造函数时 - When default constructor is required explicitly along with parameterized constructor
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM