泛型方法

泛型方法中可以定义泛型参数,此时,参数的类型就是传入的数据类型。

格式:

   访问修饰符 <泛型标识> 泛型标识 方法名称([泛型标识 参数名称]) {

}

class Generic {
    public <T> T tell(T t) {
        return t;
    }
}

public class GenericDemo03 {
    public static void main(String args[]) {
        Generic g = new Generic();
        String str = g.tell("hehehehehe");//使用String类型的str接收tell方法的返回值
        System.out.println(str);

    }

}
原文地址:https://www.cnblogs.com/sflik/p/4544896.html