我要写个方法,传一个能代表任意类型的参数,不能是Object,因为我那个参数必须是个不确定的数据类型。
比如
    public static String type(String x, type j){
      //x是接受的字符串
     for(; ;){
      try{
        j  x=(j)x;
        return x;
        break;
      }catch(Exception y){
        system.out.print("你输入的信息和类型不匹配,请重新输入:");
        continue;
      }
   }
}

解决方案 »

  1.   

    无法理解你的意思,第一个参数是 字符串,而第二个参数表示一个类的类型,那么字符串跟类的类型怎么比较呢?如果比较就是拿字符串来比较了还有你干嘛要进行一个for循环呢,似乎没有这个必要,还有返回值为String也需要说明一下不知道你传入的字符串 代表的是不是 某个类的 完整限定名比如com.a.XXX,如果那样的话就是传入一个完整限定名和对应的类类型Class 然后比较是否一致?
      

  2.   

    看LZ的意思 应该是
    public static String type(String x, Class class)
    {}
    然后可以利用反射来调用方法,不能像普通的写法那么写了
    具体的 LZ查一下反射的用法把
    reflect
      

  3.   


    /**
     * @param <T>
     * @param dataList 对象集合
     * @param T 对象
     * @param tAttrs 对象属性集合
     */
    public <T> void saveList(List<T> dataList, Class<T> T, List<String> tAttrs) {
    try {
    if (dataList != null && dataList.size() >0) {
    for (int i = 0; i < dataList.size(); i++) {
    if (tAttrs != null && tAttrs.size() != 0) {
    for (String attr : tAttrs) {
    String methodName = "get" + attr.substring(0, 1).toUpperCase() + attr.substring(1);
    Method method = T.getMethod(methodName);
    Object obj = method.invoke(dataList.get(i));
    System.out.println(obj);
    }
    }
    }
    }
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
      

  4.   

    我觉得楼主需要的可能是 这个 Joda-Convert provides a small set of classes to aid conversion between Objects and Strings. It is not intended to tackle the wider problem of Object to Object transformation.