可不可以定义一个方法供不同的泛型使用?
我不知道怎么表达了,我做了一个demo大家看一下能不能实现啊Generic.java
程序代码:
public class Generic<T> { private T key; public Generic(T key) { this.key = key; } public T getKey() { return key; } }
Hello.java
程序代码:
public class Hello { public static void main(String[] args) { Generic<Integer> genericInteger = new Generic<>(123456); Generic<String> genericString = new Generic<>("key_value"); System.out.println("key is " + doWithGeneric(genericInteger)); System.out.println("key is " + doWithGeneric(genericString)); } public static String doWithGeneric(Generic<Object> generic) { return generic.getKey().toString(); } }
但现在提示如下错误: