首页 > 解决方案 > xtend 是否支持多个构造函数?

问题描述

我正在使用 Eclipse IDE。第一个构造函数是可调用的,但不是第二个。我想知道 xtend 是否支持多个构造函数?

@Data abstract class MatchingBase implements TidilySerializable {
  val List<Integer> connections

  new (int componentSize) {
    connections = (0 ..< componentSize).toList
  }

  new (List<Integer> conn) {
    connections = new ArrayList<Integer>()
    for (int i : 0 ..< componentSize)
      connections.add(conn.get(i))
  }
}

@Data class Permutation extends MatchingBase {
}

然后,如果我new Permutation(new ArrayList<Integer>())在 Eclipse 中调用,则会将其强调为编译时错误。

标签: javaeclipsextextxtend

解决方案


是的,它支持多个构造函数。但是,我相信由于您的第二个构造函数而发生错误。在第二个构造函数的范围内,它不知道是什么componentSize。你的意思是像

new (List<Integer> conn) {
    connections = new ArrayList<Integer>()
    for (int i : 0 ..< conn.size) {
        connections.add(conn.get(i))
    }
}

查看此处的文档以获取有关 xtend 构造函数的更多信息


推荐阅读