zoukankan      html  css  js  c++  java
  • 对象值的复制

     public static <T> T obj2Obj(Object origin, Class<T> clazz) {
    Gson gson = new Gson();
    if(origin == null) { return null; } return gson.fromJson(gson.toJson(origin), clazz); }
    //把一个list对象转成含有相同属性的另外一个list
    public static <C extends Collection<T>, T> C obj2List(Object origin, Class<T> type) {
    Gson gson = new Gson();
    if(origin == null) { return null; } Type[] argumentTypes = new Type[]{type}; Type parameterizedType = ParameterizedTypeImpl.make(List.class, argumentTypes, null); return gson.fromJson(gson.toJson(origin), parameterizedType); }
    public static <T> T obj2Obj(@C("待转对象")Object origin, @C("转成的类")Class<T> clazz) {
    if(origin == null) {
    return null;
    }
    return gson.fromJson(gson.toJson(origin), clazz);
    }

    public static <C extends Collection<T>, T> C obj2List(Object origin, Class<T> type) {
    if(origin == null) {
    return null;
    }

    Type[] argumentTypes = new Type[]{type};
    Type parameterizedType = ParameterizedTypeImpl.make(List.class, argumentTypes, null);
    return gson.fromJson(gson.toJson(origin), parameterizedType);
    }
  • 相关阅读:
    Chrome快捷键统计
    数据封装
    数据抽象
    linux c++ 服务器端开发面试必看书籍(转载)
    闭包和高阶函数
    this,call,apply,bind
    DOM浏览器window对象模型
    jquery滚动条
    xml教程
    多态
  • 原文地址:https://www.cnblogs.com/coderising/p/11219530.html
Copyright © 2011-2022 走看看