构造函数中的Java条件,以便调用父构造函数

时间:2017-04-17 17:32:09

标签: java

我有以下Java类:

public class CharacteristicResponse extends VotableResponse {

    public CharacteristicResponse() {
    }

    public CharacteristicResponse(Characteristic characteristic) {
        super(characteristic);

        this.characteristicId = characteristic.getId();
        this.name = characteristic.getName();
        this.nameSlug = characteristic.getNameSlug();
        this.description = characteristic.getDescription();
        this.valueType = characteristic.getValueType();
        this.visualMode = characteristic.getVisualMode();

        ...

    }

}

我想为构造函数添加额外的参数,以便能够控制超级构造函数调用..例如:

public CharacteristicResponse(Characteristic characteristic, Boolean detailed) {
    if(detailed) {
        super(characteristic);
    }
...

}

上述示例从Java的角度来看是不可能的,因此我正在寻找一种解决方案,以便以其他方式完成。

5 个答案:

答案 0 :(得分:1)

你在评论中说过:

  

目前我有一个非常深的嵌套超级调用链。所以我想知道有一种优雅的方式可以完成它而无需重写大量代码..

就没有太多动摇而言,我基本上看到了三个选择:

  1. 如果nulldetailedfalse处理获取VotableResponse(Characteristic),则可以通过null

    super(detailed ? characteristic : null);
    
  2. 如果super()为真,您可以随时致电characteristic,然后立即通过设置器设置detailed

    super();
    if (detailed) {
        super.setCharacteristic(characteristic);
    }
    
  3. 你可以将旗帜向上传递,并让每个班级都在适当的时候处理它。

答案 1 :(得分:0)

  

从Java的角度来看,上面的例子是不可能的,所以我是   寻找解决方案如何以其他方式完成。

实际上,在Java中,超级构造函数调用是必需的。 如果你只有一个带有一些args的构造函数,你将被迫从子类中调用它来编译好 如果该类的超级构造函数不适合所有情况,则意味着您在超级构造函数中执行的操作可能不应该在此完成。

您可以将实际在父构造函数中的处理直接移动到需要它的子类构造函数,或者在多个子类需要它的情况下将其移动到公共抽象类中。

答案 2 :(得分:0)

你可以拥有一个setter方法。 或者尝试将逻辑移至工厂。 工厂设计模式 但是,如果您无法访问后端的数据结构,只需将一些参数传递给后端即可使用.setCharacteristics(Characteristic characteristic, Boolean detailed)

答案 3 :(得分:0)

如果您确实想要传入一个标志,请创建一个处理此层次结构中新对象构造的工厂:

class ResponseFactory {
  public Response buildCharacteristicResponse(Characteristic characteristic, Boolean detailed) {
    if (detailed) {
      return new DetailedCharacteristicResponse(characteristic);
    } else {
      return new SimpleCharacteristicResponse(characteristic);
    }
  }
}

class Characteristic {}

class Response {
  public Response(final Characteristic characteristic) {
  }
}

class DetailedCharacteristicResponse extends Response {
  public DetailedCharacteristicResponse(final Characteristic characteristic) {
    super(characteristic);
  }
}

class SimpleCharacteristicResponse extends Response {
  public SimpleCharacteristicResponse(final Characteristic characteristic) {
    super(characteristic);
  }
}

答案 4 :(得分:0)

我假设VotableResponse有一个空的构造函数?您的第一个示例是在super()的空构造函数中隐式调用CharacteristicResponse。您将不得不以某种方式调用超级构造函数,它必须是构造函数的第一行。

您也可以尝试使用静态实例化器

public class CharacteristicResponse extends VotableResponse {

public CharacteristicResponse() {
}

public CharacteristicResponse(Characteristic characteristic) {
    super(characteristic);

}

public static CharacteristicResponse instanceFrom(Characteristic c, boolean detailed)
    if (detailed) {
      return new CharacteristicResponse(c);
    } else {
      return new CharacteristicResponse();  
    }
}