为超级构造函数准备参数

发布于 2024-10-25 01:55:32 字数 448 浏览 1 评论 0原文

我有一个必须使用参数构造的基类。在子类中,我需要在构造基类之前准备此参数,但在 Java 中,必须在执行其他操作之前调用 super。处理这种情况的最佳方法是什么(参见下面的简单示例)。

class BaseClass {
    protected String preparedParam;

    public BaseClass(String preparedParam) {
        this.param = param;
    }
}

class ChildClass {

    public ChildClass (Map<String, Object> params) {
        // need to work with params and prepare param for super constructor
        super(param);
    }
}

I have a base class that have to be constructed with parameter. In child class I need to prepare this parameter before constructing base class but in Java super must be called before anything else. What's the best way to handle this situation (see simple example below).

class BaseClass {
    protected String preparedParam;

    public BaseClass(String preparedParam) {
        this.param = param;
    }
}

class ChildClass {

    public ChildClass (Map<String, Object> params) {
        // need to work with params and prepare param for super constructor
        super(param);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

女中豪杰 2024-11-01 01:55:32

您可以创建一个静态方法来执行转换并调用它。

class ChildClass {

    static String preprocessParams(Map<String, Object> params) {
        ...
        return someString;
    }

    public BaseClass(Map<String, Object> params) {
        super(preprocessParams(params));
    }
}

You can create an static method, that does the transformation and call that.

class ChildClass {

    static String preprocessParams(Map<String, Object> params) {
        ...
        return someString;
    }

    public BaseClass(Map<String, Object> params) {
        super(preprocessParams(params));
    }
}
长不大的小祸害 2024-11-01 01:55:32

这是一种方法:

class ChildClass {
    public ChildClass(Map<String, Object> params) {
        super(process(params));
    }

    private static String process(Map<String, Object> params) {
         // work with params here to prepare param for super constructor
    }
}

Here's one approach:

class ChildClass {
    public ChildClass(Map<String, Object> params) {
        super(process(params));
    }

    private static String process(Map<String, Object> params) {
         // work with params here to prepare param for super constructor
    }
}
赠意 2024-11-01 01:55:32

因为必须准备/初始化的许多参数很简单工厂方法对我来说是更好的解决方案。在我看来,这是更清晰的解决方案。无论如何,谢谢大家的回答。

class BaseClass {
  protected Object preparedParam;

  public BaseClass(Object preparedParam) {
    this.preparedParam = preparedParam;
  }
}

class ChildClass extends BaseClass {

  private ChildClass(Object preparedParam) {
    super(preparedParam);
  }

  public static ChildClass createChildClass(Map<String, Object> params) {
    Object param1 = params.get("param1");

    // prepare params here

    ChildClass result = new ChildClass(param1);

    // do other stuff

    return result;
  }
}

because of many parameters that have to be prepared/initialized is simple factory method better solution for me. It's little bit clearer solution at my point of view. Anyway thanks all for answers.

class BaseClass {
  protected Object preparedParam;

  public BaseClass(Object preparedParam) {
    this.preparedParam = preparedParam;
  }
}

class ChildClass extends BaseClass {

  private ChildClass(Object preparedParam) {
    super(preparedParam);
  }

  public static ChildClass createChildClass(Map<String, Object> params) {
    Object param1 = params.get("param1");

    // prepare params here

    ChildClass result = new ChildClass(param1);

    // do other stuff

    return result;
  }
}
浮萍、无处依 2024-11-01 01:55:32

我认为罗曼的回答是迄今为止最好的。
如果父类提供了默认构造函数,则可以实例化一个super的对象,然后使用setter方法。

I would rate Roman's answer as the best so far.
If the parent class provides a default constructor, you can instantiate an object of super and then use the setter method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文