Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/333.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_Class - Fatal编程技术网

Java 有没有办法用一种形式的动态类实例化来简化这个问题?

Java 有没有办法用一种形式的动态类实例化来简化这个问题?,java,class,Java,Class,我有几个扩展父类的子类,强制使用统一的构造函数。我有一个队列,它保存了这些类的列表,这些类必须扩展。我目前拥有的代码如下所示: Class<? extends MergeHeuristic> heuristicRequest = _heuristicQueue.pop(); MergeHeuristic heuristic = null; if(heuristicRequest == AdjacentMACs.class) heuri

我有几个扩展父类的子类,强制使用统一的构造函数。我有一个队列,它保存了这些类的列表,这些类必须扩展。我目前拥有的代码如下所示:

    Class<? extends MergeHeuristic> heuristicRequest = _heuristicQueue.pop();
    MergeHeuristic heuristic = null;    

    if(heuristicRequest == AdjacentMACs.class)
        heuristic = new AdjacentMACs(_parent);
    if(heuristicRequest == SimilarInterfaceNames.class)
        heuristic = new SimilarInterfaceNames(_parent);
    if(heuristicRequest == SameMAC.class)
        heuristic = new SameMAC(_parent);

这将使if语句的块变得平坦。

不幸的是,您不能强制一个类具有特定的构造函数或静态方法,这两种方法在您的情况下都非常有用

由于所有构造函数都采用相同的参数,因此还有一种方法可以使用动态类实例化简化代码:

Constructor c = heuristicRequest.getConstructor(ParentClass.class).
heuristic = c.newInstance(_parent);

请注意,您的代码不包含_parent的类类型-在我命名的代码示例中
ParentClass.class
-您必须使其适应您的代码。

您可以使用反射,但它不会使代码更漂亮

try {
    Constructor<? extends MergeHeuristic> heuristicConstructor = 
            heuristicRequest.getConstructor(_parent.getClass());
    heuristic = heuristicConstructor.newInstance(_parent);
} catch (Exception ex) {
    // TODO Handle this
}
试试看{
构造函数是
Class.forName(heuristicRequest.getName())
选项吗

然后
构造函数heuristicRequestClass.getDeclaredConstructor(_parent.getClass());


Last
heuristic=constructor.newInstance(_parent);

看起来您正在使用队列上的类作为一种标志,以指示要实例化的请求类型。另一种不使用反射的方法是,通过引入枚举来指示请求类型,并使用工厂方法,使此标志行为显式化:

public enum HeuristicType {

  AdjacentMACsHeuristic(AdjacentMACs.class) {
    @Override public MergeHeuristic newHeuristic(ParentClass parent) {
      return new AdjacentMACs(parent);
    }
  },
  SimilarInterfaceNamesHeuristic(SimilarInterfaceNames.class) {
    @Override public MergeHeuristic newHeuristic(ParentClass parent) {
      return new SimilarInterfaceNames(parent);
    }
  },
  ... // other types here.
  ;

  private final Class<? extends MergeHeuristic> heuristicClass;
  public Class<? extends MergeHeuristic> getHeuristicClass() {
    return heuristicClass;
  }

  abstract public MergeHeuristic newHeuristic(ParentClass parent);

  private HeuristicType(Class<? extends MergeHeuristic> klass) {
    this.heuristicClass = klass;
  }

}
公共枚举启发式类型{
邻接macsheuristic(邻接macs.class){
@重写公共合并启发式(父类父类){
返回新的邻接MAC(父级);
}
},
SimilariInterfaceNames启发式(similariInterfaceNames.class){
@重写公共合并启发式(父类父类){
返回新的相似接口名称(父级);
}
},
…//这里还有其他类型。
;

私人决赛Class@jlordo不,他们不会的,
heuristicRequest
是一个
对象。sry,我的眼睛比我的大脑快:为什么不使用参数化的
构造函数
?除非您铸造新实例,否则您的代码将无法工作。您是对的-您的方式更优雅。无论如何,我不明白为什么这可能是一个原因如果有人对这个有效的解决方案投反对票…你的代码有几个问题,但这个想法仍然很好。顺便说一句,我不知道你可以在枚举中声明
抽象
方法,所以感谢你教我一些新东西。将上次枚举声明后的
更改为
,然后更改构造函数uctor到
private
,它应该可以正常工作。感谢您指出这一点-这意味着为其他枚举声明留下占位符,但忘记了。_parent是一个类-无需cal getClass()OP没有说
\u parent
是一个类,在我看来像是一个局部变量,而且它不可能是
类\u parent
。你在自己的回答中这样说,“注意,你的代码没有包含\u parent的类类型…”。使用
getClass
只是另一种方法,而且更为通用。我必须测试它是否适用于参数类型的子类,但如果OP在这一行有问题,就不难对答案进行评论并询问我。是的。跳过我答案中的第一行。答案a为+1博韦。
public enum HeuristicType {

  AdjacentMACsHeuristic(AdjacentMACs.class) {
    @Override public MergeHeuristic newHeuristic(ParentClass parent) {
      return new AdjacentMACs(parent);
    }
  },
  SimilarInterfaceNamesHeuristic(SimilarInterfaceNames.class) {
    @Override public MergeHeuristic newHeuristic(ParentClass parent) {
      return new SimilarInterfaceNames(parent);
    }
  },
  ... // other types here.
  ;

  private final Class<? extends MergeHeuristic> heuristicClass;
  public Class<? extends MergeHeuristic> getHeuristicClass() {
    return heuristicClass;
  }

  abstract public MergeHeuristic newHeuristic(ParentClass parent);

  private HeuristicType(Class<? extends MergeHeuristic> klass) {
    this.heuristicClass = klass;
  }

}
Queue<HeuristicType> _heuristicQueue = ...
HeuristicType heuristicRequest = _heuristicQueue.pop();
MergeHeuristic heuristic = heuristicRequest.newHeuristic(_parent);