public void  castType(String str,String type){
    str 是 已知的字符串,
    type 是 查出来的需要转的类型,(如 integer或float不定)
    但确定str 就是 要转的 type 类型
    方法返回类型type的str的值
}

解决方案 »

  1.   

    LZ意思应该是根据传进去一个字符串和一个类型,方法里进行转换然后返回如果是前面有判断STR是什么类型再传入TYPE进行相应转换的话  方法重载应该可以实现你想要的功能。
      

  2.   

    没太理解,是这样吗?public static Object  castType(String str,String type){
    if("Integer".equals(type)){
    return Integer.valueOf(str);
    }else if("Float".equals(type)){
    return Float.valueOf(str);
    }
    return null;
    }
      

  3.   

    例如
    public float  castType("19.9",float){
    retrun ;

    public String  castType("fdada",String){
    retrun ;

    LZ是这个意思吗?
      

  4.   

    package p;public class Test {
    public static void  main(String[] args){
    System.out.println(cast("123", Integer.class));
    System.out.println(cast("123.123", Double.class));
    } public static <T extends Number> T  cast(String s, Class<T> t){
    try{
    return (T) t.getDeclaredMethod("valueOf", String.class).invoke(null, s);
    }catch(Exception e){
    throw new RuntimeException(e);
    }
    }
    }
      

  5.   

    郁闷,valueOf不是number的方法。虽然程序没错。
      

  6.   

    两种方式 一种是通过CLASS反射 另一种是你就塌塌实实的写..
      

  7.   


    public static Object  castType(String str,String type) throws Exception {
    Class obj = Class.forName("java.lang."+type);
    Method m = obj.getDeclaredMethod("valueOf",String.class);
    return m.invoke(null,str) ;
    }

    public static void main(String[] args) throws Exception {
    String str = "12";
    System.out.println(castType(str,"Integer"));
    System.out.println(castType(str,"Float"));
    }