首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Groovy构造函数在构造函数中给出了“由于哈希冲突”的编译错误

问Groovy构造函数在构造函数中给出了“由于哈希冲突”的编译错误
EN

Stack Overflow用户
提问于 2016-07-09 08:19:04
回答 1查看 325关注 0票数 0

我试图向Groovy类添加一个构造函数,但是添加第二个方法作为构造函数,我得到了一个编译错误。

无法编译类..。由于构造函数中的哈希冲突

代码语言:javascript
复制
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
}

有什么暗示我做错了什么吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-07-09 17:11:17

一旦删除泛型,就不能有两个具有相同签名的构造函数或方法。

Java也是如此。

如果您真的需要这样做,通常会有两个名称不同的静态工厂方法和一个难以访问的构造函数来处理这两种情况

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38279862

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档