首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Groovy:构造函数哈希冲突

Groovy:构造函数哈希冲突
EN

Stack Overflow用户
提问于 2018-08-10 03:22:49
回答 1查看 136关注 0票数 1

我有以下groovy代码:

代码语言:javascript
复制
def    script
String credentials_id
String repository_path
String relative_directory
String repository_url

CredentialsWrapper(script, credentials_id, repository_name, repository_group, relative_directory=null) {
    this(script, credentials_id, 'git@gitlab.foo.com:' + repository_group +'/' + repository_name + '.git', relative_directory);             
}

CredentialsWrapper(script, credentials_id, repository_url, relative_directory=null) {

    this.script         = script;
    this.credentials_id = credentials_id;
    this.repository_url = repository_url;

    if (null == relative_directory) {

        int lastSeparatorIndex  = repository_url.lastIndexOf("/");
        int indexOfExt          = repository_url.indexOf(".git"); 
        this.relative_directory = repository_url.substring(lastSeparatorIndex+1, indexOfExt);
    }
}

Jenkins给了我以下信息:

由于构造函数@第30行,第7列中的哈希冲突,无法编译类com.foo.CredentialsWrapper。

我不明白为什么,构造函数是不同的,它们没有相同数量的参数。

另外," script“是来自"WorkflowScript”的一个实例,但是我不知道应该导入什么来访问这个类,这将允许我显式地声明脚本,而不是使用"def“。

有什么想法吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-08-10 04:59:29

当您使用四个参数调用构造函数时,您希望调用第一个还是第二个参数?

如果您使用默认值编写构造函数/方法,groovy实际上将生成两个或更多版本。所以

代码语言:javascript
复制
Test(String x, String y ="test")

将导致

代码语言:javascript
复制
Test(String x, String y) {...}

代码语言:javascript
复制
Test(String x) {new Test(x, "test")}

所以你的代码想要编译成4个构造函数,但是它包含了两次带有签名CredentialsWrapper(def, def, def, def)的构造函数。如果我正确理解了您的代码,您可以省略其中一个或两个=null。结果是一样的,但是你只能得到两到三个签名。然后,您可以通过使用正确的参数计数调用它们来在两个版本之间进行选择。

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

https://stackoverflow.com/questions/51774414

复制
相关文章

相似问题

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