如何在存在多个相似的构建方法时使用构建器来初始化类?

huangapple go评论105阅读模式
英文:

How can to use builder to initialise class when there are multiple similar build methods?

问题

我想创建一个具有一些构建方法的类的实例。这是一个导入的类,因此不可编辑。当我尝试调用构建方法时,它会出现错误,指出:

重载分辨模糊。所有这些函数都匹配。
public constructor Builder<PB : Any!, MB : Any!>() defined in com.messagebus.KafkaMessageBusProducerFactory.Builder
public open fun <P : Any!, M : Any!> Builder(): KafkaMessageBusProducerFactory.Builder<UUID!, Any!>! defined in com.messagebus.KafkaMessageBusProducerFactory

我编写的代码是用 Kotlin 编写的,而 KafkaMessageBusProducer 类是用 Java 编写的。

如何解决这个冲突?

我的代码:

return KafkaMessageBusProducerFactory.Builder<UUID, Any>()
                    .kafkaAddressPropertyName("portAddress")
                    .topic(topic)
                    .messageBusTypeSerializers(serializer)
                    .build().create(true)

导入的类文件:

public class KafkaMessageBusProducerFactory<P, M> extends MessageBusProducerFactory<P, M> {
    Environment env;
    KafkaCloudAppsProperties properties;

    public KafkaMessageBusProducerFactory(KafkaMessageBusProducerFactory.Builder builder) {
        super(builder);
        this.env = builder.env;
        this.properties = new KafkaCloudAppsProperties(this.env, builder.kafkaAddressPropertyName);
    }

    public static <P, M> KafkaMessageBusProducerFactory.Builder<P, M> Builder() {
        return new KafkaMessageBusProducerFactory.Builder();
    }

    public static class Builder<PB, MB> extends com.cisco.wx2.messagebus.MessageBusFactory.Builder<KafkaMessageBusProducerFactory.Builder<PB, MB>, PB, MB> {
        Environment env;

        public Builder() {
        }

        public KafkaMessageBusProducerFactory.Builder<PB, MB> env(Environment env) {
            this.env = env;
            return this;
        }

        public KafkaMessageBusProducerFactory<PB, MB> build() {
            return new KafkaMessageBusProducerFactory(this);
        }
    }
}
英文:

I want to create an instance of class which has some build methods. It is an imported class so not editable . When I try to call the build method it gives the error saying that

Overload resolution ambiguity. All these functions match.
public constructor Builder&lt;PB : Any!, MB : Any!&gt;() defined in com.messagebus.KafkaMessageBusProducerFactory.Builder
public open fun &lt;P : Any!, M : Any!&gt; Builder(): KafkaMessageBusProducerFactory.Builder&lt;UUID!, Any!&gt;! defined in com.messagebus.KafkaMessageBusProducerFactory

The code I am writing is in Kotlin and the KafkaMessageBusProducer class is in Java.

How can I resolve this conflict ?

my code

return  KafkaMessageBusProducerFactory.Builder&lt;UUID, Any&gt;()
                    .kafkaAddressPropertyName(&quot;portAddress&quot;)
                    .topic(topic)
                    .messageBusTypeSerializers(serializer)
                    .build().create(true)

the imported class file

public class KafkaMessageBusProducerFactory&lt;P, M&gt; extends MessageBusProducerFactory&lt;P, M&gt; {
    Environment env;
    KafkaCloudAppsProperties properties;

    public KafkaMessageBusProducerFactory(KafkaMessageBusProducerFactory.Builder builder) {
        super(builder);
        this.env = builder.env;
        this.properties = new KafkaCloudAppsProperties(this.env, builder.kafkaAddressPropertyName);
    }

    
    public static &lt;P, M&gt; KafkaMessageBusProducerFactory.Builder&lt;P, M&gt; Builder() {
        return new KafkaMessageBusProducerFactory.Builder();
    }


    public static class Builder&lt;PB, MB&gt; extends com.cisco.wx2.messagebus.MessageBusFactory.Builder&lt;KafkaMessageBusProducerFactory.Builder&lt;PB, MB&gt;, PB, MB&gt; {
        Environment env;

        public Builder() {
        }

        public KafkaMessageBusProducerFactory.Builder&lt;PB, MB&gt; env(Environment env) {
            this.env = env;
            return this;
        }

        public KafkaMessageBusProducerFactory&lt;PB, MB&gt; build() {
            return new KafkaMessageBusProducerFactory(this);
        }
    }
}

答案1

得分: 0

Java中的方法名不应以大写字母开头。将方法命名为Builder会导致在Kotlin中重载Builder类的构造函数,因为Kotlin对方法和构造函数调用使用相同的语法。

如果您控制Java类,可以执行以下操作之一:

  • 修正方法名称(将其重命名为builder())。
  • 将构造函数设为私有。它对于方法来说是多余的。
  • 删除该方法,因为它对构造函数是多余的。

如果无法修改Java类,可以使用类型别名来指定和调用构造函数。(如果该方法执行的是与构造函数不同的操作,并且您需要调用该方法,我不知道您能做什么。)

typealias BuilderClass<P, M> = KafkaMessageBusProducerFactory.Builder<P, M>;

//...

return BuilderClass<UUID, Any>()
            .kafkaAddressPropertyName("portAddress")
            .topic(topic)
            .messageBusTypeSerializers(serializer)
            .build().create(true);
英文:

Method names in Java should not start with a capital letter. Naming the method Builder causes it to overload the Builder class constructor in Kotlin, since Kotlin uses the same syntax for method and constructor calls.

If you have control of the Java class, you can do one of the following:

  • Fix the name of your method (rename it builder())
  • Make the constructor private. It is redundant to the method anyway.
  • Remove the method, since it is redundant to the constructor.

If you cannot modify the Java class, you could use a type alias to specify and call the constructor. (If the method did something different than the constructor and you needed to call the method, I don't know what you'd be able to do.)

typealias BuilderClass&lt;P, M&gt; = KafkaMessageBusProducerFactory.Builder&lt;P, M&gt;

//...

return  BuilderClass&lt;UUID, Any&gt;()
            .kafkaAddressPropertyName(&quot;portAddress&quot;)
            .topic(topic)
            .messageBusTypeSerializers(serializer)
            .build().create(true)

</details>



huangapple
  • 本文由 发表于 2020年8月26日 18:45:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/63595942.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定