英文:
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<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
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<UUID, Any>()
.kafkaAddressPropertyName("portAddress")
.topic(topic)
.messageBusTypeSerializers(serializer)
.build().create(true)
the imported class file
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);
}
}
}
答案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<P, M> = KafkaMessageBusProducerFactory.Builder<P, M>
//...
return BuilderClass<UUID, Any>()
.kafkaAddressPropertyName("portAddress")
.topic(topic)
.messageBusTypeSerializers(serializer)
.build().create(true)
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论