62,615
社区成员
发帖
与我相关
我的任务
分享
public <T> void findALL(){
System.out.println("type is "+***);//***的位置希望得到T的类型的字符串
}
public class Test {
public String findAll() throws SecurityException, NoSuchMethodException {
Test t = new Test();
String type = t.getClass().getMethod("findAll").getReturnType().getName();
System.out.println(type);
return "aa";
}
public static void main(String[] args) throws SecurityException, NoSuchMethodException {
Test t = new Test();
t.findAll();
}
}
public <T> void getInstance(Class<T> clazz){
System.out.println(clazz.getName());
package test;
import java.util.ArrayList;
public class JustTest {
private static <T> T getInstance(Class<T> clazz){
System.out.println(clazz.getName());
try {
return clazz.newInstance();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return null;
}
public static void main(String[] args){
String str = JustTest.getInstance(String.class);
ArrayList list = JustTest.getInstance(ArrayList.class);
}
}
package test;
import java.util.ArrayList;
public class JustTest {
private static <T> void getInstance(Class<T> clazz){
System.out.println(clazz.getName());
}
public static void main(String[] args){
JustTest.getInstance(String.class);
JustTest.getInstance(ArrayList.class);
}
}
public class Gen<T> {
private T ob; //定义泛型成员变量
public Gen(T ob) {
this.ob = ob;
}
public T getOb() {
return ob;
}
public void setOb(T ob) {
this.ob = ob;
}
public void showTyep() {
System.out.println("T的实际类型是: " + ob.getClass().getName());
}
}
public <T> T findALL(){
return 一个T类型的实例;
}
public class j {
static <T> void findAll(T t){
System.out.println(t.getClass());
}
public static void main(String[] args) {
findAll("123");
findAll(123);
findAll(new j());
}
}
public class Test {
public String findAll() throws SecurityException, NoSuchMethodException {
Test t = new Test();
String type = t.getClass().getMethod("findAll").getReturnType().getName();
System.out.println(type);
return "aa";
}
public static void main(String[] args) throws SecurityException, NoSuchMethodException {
Test t = new Test();
t.findAll();
}
}