【问题标题】:Conditional calling of a constructor构造函数的条件调用
【发布时间】:2018-01-30 15:43:44
【问题描述】:

我的情况类似于this

如果我仍然想有条件地调用构造函数怎么办? (虽然他们说创建单独的类是可行的)

需求结构:

超类:

 public class Super
    {
        public Super(DTO1 dto1Object){
               this.dto1Object = dto1Object;     
          }

        public Super(DTO2 dto2Object)){
               this.dto2Object = dto2Object;     
          }
    }

派生类:

 public class Derived extends Super
    {
        public Derived(Object obj)
        {
           //some_condition to check if passed object obj is dto1Object                 
           //do something with dto1Object

           //some_condition to check if passed object is dto2Object                               
           //do something with dto2Object


        }
    }

我应该如何实现它?

编辑:

根据以下建议以这种方式实现:

超类:

 public class Super
    {
        protected static DTO1 dto1Obj;
        protected static DTO2 dto2Obj;

        public Super(DTO1 dto1Object){
               this.dto1Object = dto1Object;     
          }

        public Super(DTO2 dto2Object)){
               this.dto2Object = dto2Object;     
          }
    }

派生类:

    public class Derived extends Super
    {
        public Derived(DTO1 dto1Object){ super(dto1Object); }
        public Derived(DTO2 dto2Object){ super(dto2Object); }

        public static Derived create(Object obj) {

           if (obj.equals(dto1Obj) {
             return new Derived((DTO1) obj);
           }

           if (obj.equals(dto2Obj) {
             return new Derived((DTO2) obj);
           }

           // ...

          private String Function(String str){
            if(create(dto1Obj).equals(dto1Obj) { 
             //do something
            }
            else if(create(dto2Obj).equals(dto2Obj)){
             //do something else 
            }
            return str;
          }

        }
    }

EDIT2:

根据下面的建议,这是使用instanceof 的正确方法吗?

if (create(dto1Obj) instanceof DTO1) {
          //something
        }
        else if(create(dto2Obj) instanceof DTO2) {
          //something else
        }

显示以下错误:

Incompatible conditional operand types Derived and DTO1
Incompatible conditional operand types Derived and DTO2

【问题讨论】:

  • 你不能在构造函数中,因为super(...)必须是第一个语句。
  • 你不能,基本上。要么你需要添加一个Super(Object) 构造函数,要么在Derived 中创建一个静态工厂方法和两个单独的私有构造函数。
  • 是的。只需使用静态工厂 - 根据“有效 Java”,这是一个很好的做法

标签: java constructor instanceof constructor-overloading


【解决方案1】:

你不能在构造函数中,因为super(...) 必须是第一条语句。

我能想到的唯一方法是使用静态工厂方法,并调用构造函数的特定于类的重载:

public class Derived extends Super
{
    private Derived(DTO1 dto1Object){ super(dto1Object); }
    private Derived(DTO2 dto2Object){ super(dto2Object); }

    public static Derived create(Object obj) {
       //some_condition to check if passed object obj is dto1Object                 
       //do something with dto1Object
       if (someCondition) {
         return new Derived((DTO1) obj);
       }

       //some_condition to check if passed object is dto2Object                               
       //do something with dto2Object
       if (someOtherCondition) {
         return new Derived((DTO2) obj);
       }

       // ...?
    }
}

【讨论】:

  • 完美!谢谢!我有一个快速的问题(可能是一个愚蠢的问题)我可以这样做来检查 obj 是 dto1Obj 还是 dto2Obj? if(create(dto1Obj).equals(dto1Obj)){ //做点什么 } else { //做点别的 }
  • 您可以使用instanceof... 但如果这是您尝试做的唯一检查,为什么不直接公开构造函数呢?然后在编译时检查类型。
  • 你是指我编辑帖子的方式吗? (无法在此处发布代码,因此我在问题中添加为编辑)
猜你喜欢
  • 2013-05-22
  • 1970-01-01
  • 1970-01-01
  • 2017-01-01
  • 2013-11-18
  • 2017-07-19
  • 1970-01-01
  • 2011-07-18
  • 2011-04-16
相关资源
最近更新 更多