public void test(Object o,String typeName)
{
  //在这里如何将o转为指定 String 或 int 或double 类型
  //从而可动态去调用  checkValueOK() 来适应。
  
  // 不想用 (String)o  (int)o 转换的方法,想通过typeName 类名来动态将object转为typeName指定的类型
  // typeName 传入的值类似 "java.lang.String"  "int"  "double"
}  /**校验值为有效值*/
    public static  boolean checkValueOK(String value)
    {
            return value != null;
    }
    public static  boolean checkValueOK(int value)
    {
            return value != -Integer.MAX_VALUE;
    }
    public static  boolean checkValueOK(double value)
    {
            return value != -Double.MAX_VALUE;
    }

解决方案 »

  1.   

    用Class.forName(String s).newInstance();
    s表示类型名,比如s="java.lang.String";
      

  2.   

    Class.forName(String s).newInstance();
      

  3.   

    楼主,int、double不是类,是基本数据类型,对应的类是Integer、Double
      

  4.   

    Class.forName得到的也是object啊。我觉得java很难实现(能实现吗?),首先声明这个指定的类型就是问题。
      

  5.   

    用这样的方法动态转换类型,很难实现,能不能实现目前也不知晓,建议楼主采用其它的方法吧,
    可以参考一下下面的,如果楼主觉得合适就用,public static boolean checkValueOK(Object value)
       {
           try
           {
               if (Class.forName("java.lang.Double").isInstance(value))
               {
                   return ((Double)value).doubleValue() != -Double.MAX_VALUE;
               }
               else if (Class.forName("java.lang.Integer").isInstance(value))
               {
                   return ((Integer)value).intValue() != -Integer.MAX_VALUE;
               }
               else
               {
                   return value != null;
               }
           }
           catch (ClassNotFoundException nex)
           {
           }
           return false;
       }
      

  6.   

    你说的那种不知道怎么做,我写了一个,楼主可以参考一下!
    public class Test{
    public static boolean test(Object o){

      
        if(o instanceof String){
                return  (String)o!= null;
        }
        if(o instanceof Integer){
                return  (Integer)o!= -Integer.MAX_VALUE;
        }
        if(o instanceof Double ){
                return (Double)o != -Double.MAX_VALUE;
        }
        return true;
      }
        public static void main(String[] args){
         System.out.println(test("s"));
         System.out.println(test(new Integer(5)));
         System.out.println(test(new Double(4d)));
        }
    }
      

  7.   

    楼上的结构简单,比较合用,只是有一点点小意外
    我来改一下public static boolean test(Object o)
    {
        if(o instanceof String){
                    return  (String)o!= null;
            }
            if(o instanceof Integer){
                    return  ((Integer)o).intValue()!= -Integer.MAX_VALUE;
            }
            if(o instanceof Double ){
                    return ((Double)o).doubleValue() != -Double.MAX_VALUE;
            }
        return true;
      }
      

  8.   

    用if或switch判断typeName再做转换
    if (typeName.equals("string))
    {
        (String)o;
    }
    else if (typeName.equals("int"))
    {  (Integer)o;
    }
      

  9.   

    真的没有其实方法了吗。。 那Hibernet是怎么实现映射的。。我想Hibernet的转换应该比这复杂。。
      

  10.   


    你的思路好像不合适吧?如果你的方法可以运行的话:test (猫的实例,狗的类型);
    把猫变成狗也就成立了.小心 ClassCastException 满天飞。
      

  11.   

    jdk1.5里面有泛型, 实现起来简单有效, 而1.5之前的办法只有楼上提到的两种了:下校正和反射
      

  12.   

    import java.lang.reflect.*;
    public class Test
    {
    public static void main(String[] args)
    {
    try
    {
    new Test().test("test","java.lang.String");
    new Test().test(new Integer(44),"java.lang.Integer");
    new Test().test(new Double(44.0d),"java.lang.Double");

    }
    catch(Exception e){e.printStackTrace();}
    }
    public void test(Object o,String typeName) throws ClassNotFoundException,IllegalAccessException,InvocationTargetException
    {
      Object rt=null;
      Class c=Class.forName(typeName);
      Class tc=this.getClass();
      Method[] m=tc.getDeclaredMethods();
      for(int i=0;i<m.length;i++)
      {
    //System.out.println(m[i].toString());
    Class[] mc=m[i].getParameterTypes();
    if(mc[0]==c)
    {
    rt=m[i].invoke(this,o);
    System.out.println("invoke  "+m[i].toString());
    }
      }
    }  /**校验值为有效值*/
        public static  boolean checkValueOK(String value)
        {
                return value != null;
        }
        public static  boolean checkValueOK(Integer value) //为了实现,我把原始类型改为了包装类型!!!    {
                return value.intValue()!= -Integer.MAX_VALUE;
        }
        public static  boolean checkValueOK(Double value)
        {
                return value.doubleValue()!= -Double.MAX_VALUE;
        }
    }
      

  13.   

    使用RTTI:
    //在containerTypes 中存储所有可能进行转换的类型.
    final Class[] containerTypes = { String.class, Integer.class, Double.class};
    void  test(Object o,String typeName) {
      for (int j = 0; j < containerTypes.length; ++j) {
      if (containerTypes[j].isInstance(o)) {
      switch (j){
    case 0: checkValueOK((String)o );break;
    case 1: checkValueOK((Integer)o) ; break;
    case 2: checkValueOK((Double)o) ; break;
      }
       break;
      }
       }
    }
      

  14.   

    其实参数String typeName可以不需要
      

  15.   

    xiaohuasz() 说得对,可以按他说的做,应该没有问题!!!