Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/400.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/102.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
构造函数上的Java条件,以便调用父构造函数_Java - Fatal编程技术网

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

构造函数上的Java条件,以便调用父构造函数,java,Java,我有以下Java类: public class CharacteristicResponse extends VotableResponse { public CharacteristicResponse() { } public CharacteristicResponse(Characteristic characteristic) { super(characteristic); this.characteristicId =

我有以下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的角度来看,上面的例子是不可能的,所以我正在寻找一个解决方案,如何以其他方式实现它

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

实际上,在Java中,超级构造函数调用是强制性的。 如果您只有一个带有一些参数的构造函数,那么您必须从子类调用它来编译fine
如果类的超级构造函数不适用于所有情况,则可能意味着不应该在这里执行超级构造函数中的操作。


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

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

在您的评论中:

目前我有一个相当深的嵌套超级调用链。。所以我想知道有没有一种优雅的方法,不需要重写很多代码就可以完成

就不太动摇这一点而言,我认为基本上有三种选择:

  • 如果
    detailed
    false
    且具有
    VotableResponse(特征)
    句柄获取
    null
    ,则可以传递
    null

    super(detailed ? characteristic : null);
    
  • 如果
    detailed
    为true,则始终可以调用
    super()
    ,然后立即通过setter设置
    characteristic

    super();
    if (detailed) {
        super.setCharacteristic(characteristic);
    }
    
  • 您可以向上传递该标志,并让沿途的每个类适当地处理它


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

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

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

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

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

    就目前而言,这是没有意义的——您必须在Java中调用超级构造函数。这一定是有原因的;你能进一步解释一下吗?如果细节是假的,你希望发生什么?调用无参数超级构造函数?这是一个DTO对象,因此我将从UI传递
    detailed
    param,以便能够控制对象初始化的级别。这样我就可以在不需要的时候在后端节省一些资源。按照你的想法,为什么不从父构造函数中删除逻辑并将其移动到另一个方法?@alexanoid:grateant?不是从它的声音来看。:-)如果
    detailed
    false
    且具有
    VotableResponse(特征)
    句柄获取
    null
    super(detailed?特征:null)
    或者,如果
    detailed
    为真,则始终调用
    super()
    ,然后立即通过setter设置
    characteristic
    。或者在链上传递标志,并让沿途的每个类适当地处理它。。。