在同一个类中构造函数链的用途是什么?

时间:2016-08-08 07:03:57

标签: java oop object constructor this

考虑以下代码段

public

我在同一个类中调用构造函数的位置。

已在某处阅读

private XYZOuputStream(String prodid, String productname) {
    this.productid = prodid;
    this.productname = (productname == null) ? DEFAULT_PRODUCT_NAME : productname;
}

private XYZOuputStream(String productid, String path, String productname, String password) throws Exception {
    this(productid, productname);
    this.filepath = path;
    this.password = password;
    initClient();
}

public XYZOuputStream(String productid, String password, String path, String productname, boolean overwrite, boolean iscompressed) throws Exception {
    this(productid, path, productname, password);
    this.overwrite = overwrite;
    this.iscompressed = iscompressed;
    initOuputStream();
}
private void initClient() throws Exception {
    this.dbClient = clientPool.getClient(this.productid, this.productname);     
}

public constructor_name(String name) { this.test_name = name; System.out.println("Name printed is :::"+ this.test_name); } 的上述用法会造成麻烦,因为对象创建尚未完成。

请澄清以下内容:

  1. 使用this.name是否会导致问题,因为尚未创建对象?和
  2. 上述在构造函数中调用构造函数的方法是否正确? (即;我是否在正确的意义上使用同一类中的构造函数链接?)
  3. 我不确定我是否可以在这里发布,因为它是为了清楚概念而不是编程: - (

1 个答案:

答案 0 :(得分:0)

  

这种this.name的使用是否会导致问题,因为尚未创建对象?

public constructor_name(String name) {
    name = "Rabbit";
    System.out.println("Name printed is :::"+ this.name);
}

1)可能会导致代码出现逻辑错误。调用System.out.println时,将使用参数name,其值为Rabbit,但this.name为类字段。所以它会打印出它拥有的任何价值。如果该字段已使用默认值以外的其他值初始化,例如String name = "Bird";,则会打印Bird,否则将打印null注意!如果超类有字段name且子类没有覆盖它,那么即使您使用关键字this引用,超类name也将是使用

2)此签名中带参数name的构造函数也没有任何意义。因为在它的身体中你将它的值重新分配给"Rabbit",所以没有任何关系可以传递任何东西,那么public constructor_name()将是更好的选择。

3)如果您需要根据其他字段初始化字段,请执行以下操作

public constructor_name(String x, String y) {
    this.x = x;
    this.y = y;
    this.z = x + y; // or this.z = this.x + this.y;
    System.out.println("Sum is :::"+ this.z);
}
  

上面在构造函数中调用构造函数的方法是否正确?

不,不是。一个类应该只有一个主构造函数。其他构造函数应该调用primary构造函数。但在您的示例中,每个构造函数都调用不同的构造函数(一个参数extra)。

要解决此问题,请让我们声明您的主要构造函数

public XYZOuputStream(String productid, String password, String path, String    productname, boolean overwrite, boolean iscompressed) throws Exception {
    this.productid = productid;
    this.path = path;
    this.productname = (productname == null) ? DEFAULT_PRODUCT_NAME :    productname;
    this.password = password;
    this.overwrite = overwrite;
    this.iscompressed = iscompressed;
    // if(someCondition) for example path != null call initOutputStream();
    // initOuputStream();

}

然后所有其他构造函数都会调用它。

private XYZOuputStream(String productid, String path, String productname,   String password) throws Exception {
    this(productid, password, path, productname, false, false)
    initClient();
}

private XYZOuputStream(String productid, String productname) throws Exception {
    this(productid, DEFAULT_PASSWORD, DEFAULT_PATH, productname, false, false)
} 

注意!您需要为主构造函数提供默认参数,该构造函数在包含构造函数时没有相应的参数,例如boolean overwrite参数获取false参数。

相关问题