简体   繁体   中英

java generic function parameter type mismatch for “Object” class

I've got following code snippet:

class pp<K, V> {
    public pp(K k, V v) {
        this.k = k;
        this.v = v;
    }

    private K k;
    private V v;
}
class my{
    public static void f(pp<? extends Object, ? extends Object> p){
        System.out.println(p);
    }
}
public class genericFunction {
    public static void main(String[] args) {
        my.f("abc", new Integer(2));//compilation failure
    }
}

It fails to compile inside the main. Saying looking for

pp<? extends java.lang.Object,? extends java.lang.Object>

but found

java.lang.String,java.lang.Integer

How to fix it?

You don't have a pp , you have two arguments.

Put them in your object type: my.f(new pp<>("abc", new Integer(2))) .

Because you are passing literal arguments (String and Integer) instead of passing an object of pp .

Try my.f(new pp<>("abc", new Integer(2)))

Side note: use Pascal casing for type names

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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