我试图向Groovy类添加一个构造函数,但是添加第二个方法作为构造函数,我得到了一个编译错误。
无法编译类..。由于构造函数中的哈希冲突
class BaseException extends RuntimeException {
private Integer status
private String message
private Long timestap
private List<ErrorMessage> errors
BaseException(Integer status, String message, List<ErrorMessage> errorMessageList) {
this.status = status
this.message = message
this.timestap = System.currentTimeMillis();
this.errors = errorMessageList
}
// ---
// adding the method below gives a compile error
// ---
BaseException(Integer status, String message, List<ErrorCode> errorCodeList) {
this.status = status
this.message = message
this.timestap = System.currentTimeMillis();
this.errors = []
errorCodeList.each { error ->
this.errors.add(new ErrorMessage(error.code, error.description))
}
}
.. code emitted
}有什么暗示我做错了什么吗?
发布于 2016-07-09 17:11:17
一旦删除泛型,就不能有两个具有相同签名的构造函数或方法。
Java也是如此。
如果您真的需要这样做,通常会有两个名称不同的静态工厂方法和一个难以访问的构造函数来处理这两种情况
https://stackoverflow.com/questions/38279862
复制相似问题