【问题标题】:Pass Class as parameter and return same class object传递类作为参数并返回相同的类对象
【发布时间】:2013-09-04 09:02:13
【问题描述】:

我想将 Class 作为参数传递并返回与参数相同的类。 参数可以是任何类。所以我想要 comman 方法,其中我只需要传递特定类的对象,它会给我与返回相同的对象。 我知道使用 Java - 泛型或使用 java 集合是可能的,但我不知道如何?

代码:

 public Class_object_as_paramter(parent) webCall(Class Obj as parameter(parent))
                throws JsonSyntaxException, IOException, XmlPullParserException {
            GsonBuilder gsonBuilder = new GsonBuilder();
            Gson gson = gsonBuilder.create();

            parent = (Collection<?>) gson.fromJson(SoaplCalls
                    .GetUserwiseSalesContracts(GsonExample.this, "55000024"),
                    ParentSalesContract.class (also has to pass same class here);
            return parent;
        }

【问题讨论】:

    标签: java android generics collections


    【解决方案1】:

    像这样创建通用方法:

    <T> T method(T t) {
        ...
        T tt = ...
        return tt;
    }
    

    【讨论】:

      【解决方案2】:
      public <T> T webCall(T type)
      {
         return type;  
       }
      

      【讨论】:

        【解决方案3】:
        public class GenericsExample <T> {
          public T webCall(T parent) {
            // ...
            T ret = ??; // Might be parent, might be a new class...
            return ret;
          }
        }
        

        【讨论】:

          猜你喜欢
          • 2013-03-24
          • 2022-07-10
          • 1970-01-01
          • 2023-03-27
          • 1970-01-01
          • 2014-05-13
          • 2019-08-15
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多