如何将泛型表示的类型转换为字符串

daizhe 2010-02-17 01:23:45
例如:

public <T> void findALL(){
System.out.println("type is "+***);//***的位置希望得到T的类型的字符串
}


能实现么?
...全文
549 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
bobo364 2010-02-19
  • 打赏
  • 举报
回复
上面是j2se.phpchinaz.cn的例子
bobo364 2010-02-19
  • 打赏
  • 举报
回复
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();
}
}
fatRoach 2010-02-19
  • 打赏
  • 举报
回复
传一个Clazz<T>作为参数


public <T> void getInstance(Class<T> clazz){
System.out.println(clazz.getName());
daizhe 2010-02-19
  • 打赏
  • 举报
回复
百度的例子看过了,它是传了个对象进来,我是想直接通过T得到名称
fatRoach 2010-02-19
  • 打赏
  • 举报
回复
如果你想根据class生成新实例的话

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);
}
}
fatRoach 2010-02-19
  • 打赏
  • 举报
回复
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);
}
}
daizhe 2010-02-19
  • 打赏
  • 举报
回复
12楼和1楼的一样吧?而且都需要.getReturnType()方法,这个方法是得到返回值类型么?可这里T不是findAll的返回值啊!?
另外
引用 11 楼 fatroach 的回复:
传一个Clazz <T>作为参数

Java codepublic<T>void getInstance(Class<T> clazz){
System.out.println(clazz.getName());

中这个Class<T>参数如何调用?
musiclee 2010-02-18
  • 打赏
  • 举报
回复
百度的例子

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());
  }
  }

daizhe 2010-02-17
  • 打赏
  • 举报
回复
别管有没有意义,能实现不?
xingda1989 2010-02-17
  • 打赏
  • 举报
回复
我到觉得这样的需求没什么意义
knightzhuwei 2010-02-17
  • 打赏
  • 举报
回复
这样的需求估计是不可能实现的
daizhe 2010-02-17
  • 打赏
  • 举报
回复
好吧,看来我得写完整点,这个例子应该是这样的

public <T> T findALL(){
return 一个T类型的实例;
}

但是创建这个T类型的实例时要想用Class.forName()方法,参数需要是String类型的类名,那么如何得到这个String类型的类名呢,还有就是实例只能在这个findAll方法中创建,不会有别的方法给它传一个实例
knightzhuwei 2010-02-17
  • 打赏
  • 举报
回复

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());
}
}
daizhe 2010-02-17
  • 打赏
  • 举报
回复
而且findAll方法的返回值类型是void啊
daizhe 2010-02-17
  • 打赏
  • 举报
回复
有没有简单点的办法?
xingda1989 2010-02-17
  • 打赏
  • 举报
回复

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();
}
}





很简陋的写了一个

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧