简体   繁体   English

无法使用Java泛型推断类型

[英]Type cannot be inferred using java generics

I have two types(T,M) first one is recognized by the compiler second one isnt. 我有两种类型(T,M),第一种是编译器识别的,第二种不是。 any idea what am i doing wrong? 知道我在做什么错吗?

public class SendMessageResponse<T> {

    private T result;
}

public class MessageResponse<M> {

    private M message;
}

interface is defined this way: 接口是这样定义的:

public interface EventQueue<T, M> {

    SendMessageResponse<T> send(String address, String message, String eventId);

    void consumeBatch(String address, int numOfConsumersPerQueue, Handler<MessageResponse<M>> callback);

    ..
}

on impl: 暗示上:

public class EventQueueSQS
        implements EventQueue<SendMessageResultImpl, MessageImpl> {

    @Override
    public SendMessageResponse send(String address, String message, String eventId) {..}

    @Override
    public void consumeBatch(String address, int numOfConsumerThreads, Handler<MessageResponse> callback) {
    }

    ..
}

for the consumeBatch override method i have complication error: 对于consumerBatch覆盖方法,我有复杂错误:

Error:(164, 17) java: name clash: consumeBatch(java.lang.String,int,io.vertx.core.Handler<java.util.List<MessageResponse>>) in EventQueueSQS and consumeBatch(java.lang.String,int,io.vertx.core.Handler<java.util.List<MessageResponse<M>>>) in EventQueue have the same erasure, yet neither overrides the other

compiler force me on the method to write the impl: 编译器强迫我编写该impl的方法:

public void consumeBatch(String address, int numOfConsumersPerQueue, Handler<MessageResponse<Message>> callback) {

why? 为什么? I have declared it on the class level 我已经在全班宣布

any idea? 任何想法?

Perhaps the issue that Iam using Handler which holds class that has generics? 也许Iam使用持有具有泛型类的Handler的问题? I cant find the diff between SendMessageResponse or using Handler> regarding generics as the first one works and the other one doesnt 我找不到在SendMessageResponse或使用Handler>关于泛型之间的区别,因为第一个可行,而另一个没有

When compiled to bytecode type parameters such as M are transformed to their bound implementation type if bounded or Object if unbounded see this .Since you provide no implementation type in the method of your class EventQueueSQS , both the interface method and the class method are compiled to Handler<MessageResponse<Object>> which creates a clash error. 当编译成字节码类型的参数,如M被转换为它们的结合实现的类型,如果有界或对象 ,如果无界看到这个 。既然你提供你的类的方法,没有实现类型EventQueueSQS ,无论是接口方法和类方法编译成Handler<MessageResponse<Object>> ,它创建一个冲突错误。 So you need to provide-bind an implemented type for your type parameter M in your class method,which in your case is MessageImpl. 因此,您需要在类方法中为类型参数M提供绑定的实现类型,在本例中为MessageImpl。

If you dont want to provide an actual implementation type in your class EventQueueSQS either remove <M> from Handler<MessageResponse<M>> in the interface, or make your class also parameterized like EventQueueSQS<T,M> implements EventQueue<T,M> where you still have to provide it inside your method's argument but as a parameter M , and this way you can create instances like EventQueueSQS<SendMessageResultImpl,MessageImpl> . 如果您不想在类EventQueueSQS提供实际的实现类型,请从接口中的Handler<MessageResponse<M>>中删除<M> ,或者也像EventQueueSQS<T,M> implements EventQueue<T,M>这样对类进行参数化EventQueueSQS<T,M> implements EventQueue<T,M>您仍然需要在方法的参数中(但作为参数M提供它,这样您就可以创建诸如EventQueueSQS<SendMessageResultImpl,MessageImpl>类的实例。

In your method you set the argument type: 在您的方法中,设置参数类型:

Handler<MessageResponse> callback

But this is the same as 但这与

Handler<MessageResponse<Object>> callback

and this does conflict with your type M which is MessageImpl . 这确实与您的M类型(即MessageImpl冲突。 If you are using a Java version supporting the diamond operator, it may allow you to write Handler<MessageResponse<>> callback which is the same as the compiler-suggested one. 如果使用支持Diamond运算符的Java版本,则可以允许您编写Handler<MessageResponse<>> callback ,该Handler<MessageResponse<>> callback与编译器建议的Handler<MessageResponse<>> callback相同。

Here is your concrete implementation: 这是您的具体实现:

public class EventQueueSQS
    implements EventQueue<SendMessageResultImpl, MessageImpl> {

    @Override
    public SendMessageResponse send(String address, String message, String eventId) {..}

    @Override
    public void consumeBatch(
            String address, 
            int numOfConsumerThreads, 
            Handler<MessageResponse> callback) {
    }

    ..
}

According to your interface definition, this is not an appropriate match for the signature of your consumeBatch method. 根据您的接口定义,这与您的consumeBatch方法的签名不匹配。 You need: 你需要:

    public void consumeBatch(
            String address, 
            int numOfConsumerThreads, 
            Handler<MessageResponse<MessageImpl>> callback) {
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM