gson通过TypeToken实现了对泛型数据的支持,使用方式如下:

gson.fromJson([待转化的字符串], new TypeToken<[目标类]<目标类中的泛型>>(){}.getType())

创建一个类实例,new [目标类]()就够了,TypeToken后面的{}是做什么呢,不少人可能像我一样有困惑。

先看下TypeToken的构造方法

protected TypeToken() {
this.type = getSuperclassTypeParameter(getClass());
this.rawType = (Class<? super T>) $Gson$Types.getRawType(type);
this.hashCode = type.hashCode();
}
 static Type getSuperclassTypeParameter(Class<?> subclass) {
Type superclass = subclass.getGenericSuperclass();
if (superclass instanceof Class) {
throw new RuntimeException("Missing type parameter.");
}
ParameterizedType parameterized = (ParameterizedType) superclass;
return $Gson$Types.canonicalize(parameterized.getActualTypeArguments()[0]);
}

这里我们只关注标红的部分。为了方便说明,我们写一个测试类,使用上面的2个方法,并打印出来。

package com.ym.materials.jdk;

import org.junit.Test;

/**
* Created by ym on 2018/6/30.
*/
public class ConstructorDiff { public static class ClassRoom{
} public static class User<T>{
private T room; public User() {
System.out.println(this.getClass());
System.out.println(this.getClass().getGenericSuperclass());
}
} @Test
public void testSimpleConstructor() throws Exception {
new User<ClassRoom>();
} @Test
public void testConstructorWithBrace() throws Exception {
new User<ClassRoom>(){};
}
}

执行结果如下:

class com.ym.materials.jdk.ConstructorDiff$User
class java.lang.Object
class com.ym.materials.jdk.ConstructorDiff$1
com.ym.materials.jdk.ConstructorDiff.com.ym.materials.jdk.ConstructorDiff$User<com.ym.materials.jdk.ConstructorDiff$ClassRoom>
testConstructorWithBrace不是User,而是$1,$前缀是jdk隐藏内部类的命名规则,所以答案已经明了了,{}不是直接调用构造去创建实例,而是先创建一个类,再去实例化。等价于定义一个$1的类结构,再去实例化。
public class $1 extends User<ClassRoom> {
} @Test
public void test$1() throws Exception {
new $1();
}

输出结构与testConstructorWithBrace一致。

new关键字创建对象带不带{}的区别的更多相关文章

  1. 用cp命令拷贝文件,源目录后带不带斜杠的区别

    当我还是Linux超级傻白的时候,需要拷贝一个很大的数据集,然后再拷贝源文件夹的后面跟了一个前倾斜杠,然后就发现居然拷贝的是整个文件夹里的东西,而不是文件夹本身.事儿倒是不大,我重新建一个文件夹,把这 ...

  2. Java 中带参带返回值方法的使用

    如果方法既包含参数,又带有返回值,我们称为带参带返回值的方法. 例如:下面的代码,定义了一个 show 方法,带有一个参数 name ,方法执行后返回一个 String 类型的结果 调用带参带返回值的 ...

  3. phpqrcode 分别生成普通、带logo、带背景带logo二维码

    前提:下载好 phpqrcode 类库 1.生成普通二维码 //引入类库 include('phpqrcode/phpqrcode.php'); //二维码里面的链接地址 $url="htt ...

  4. 使用new关键字创建对象数组(C#,C++,Java)

    今天遇到一个题目 分析下面的代码,判断代码是否有误. using System; namespace Test1 { class Point { public int x; public int y; ...

  5. 【python基础语法】常用内置函数、关键字、方法和之间的区别(小结)

    ''' 关键字: False:bool数据类型 True:bool数据类型 None:表示数据的内容为空 and:逻辑运算符:与 or:逻辑运算符:或 not:逻辑运算符:非 in:身份运算符,判断变 ...

  6. TP框架中生成带背景带文字的二维码

    首先下载一个phpqrcode的包放到/vendor目录下 链接:https://pan.baidu.com/s/18jV9DypYB_PHDhD6C0iedQ 提取码:qxuo 如果只是单纯生成二维 ...

  7. 带不带protype的区别

    总结写在前面: ①:带有protype:表示类的扩展,必须new后才能使用. ②:不带protype:属于静态方法,直接调用即可. html代码: <!DOCTYPE html> < ...

  8. 星环大数据安全组件Guardian与hadoop自带的安全组件区别

    在进行讲解之前,先带大家学习下hadoop关于hdfs自己的安全如何实现的--------------------------- 名词: ACL-访问控制列表(Access Control List, ...

  9. url后面带斜杠与不带斜杠的区别

    比如: https://www.baidu.com/test/ https://www.baidu.com/test 当Web服务器接收到对某个末尾不含斜杠的url请求时,例如https://www. ...

随机推荐

  1. python入门之文件处理

    1.读取文件 f=open(file="C:\BiZhi\新建文本文档.txt",mode="r",encoding="utf-8") da ...

  2. 2018.11.01 洛谷P3953 逛公园(最短路+dp)

    传送门 设f[i][j]f[i][j]f[i][j]表示跟最短路差值为iii当前在点jjj的方案数. in[i][j]in[i][j]in[i][j]表示在被选择的集合当中. 大力记忆化搜索就行了. ...

  3. Latex插图操作

    1.竖排插入两张图 \begin{figure}[h] //放在当前位置 \centering \subfigure[A given traffic flow set]{ \includegraphi ...

  4. lf-8.4 数据的增删改

    MySQL数据操作: DML 在MySQL管理软件中,可以通过SQL语句中的DML语言来实现数据的操作,包括 使用INSERT实现数据的插入 UPDATE实现数据的更新 使用DELETE实现数据的删除 ...

  5. IntelliJ IDEA 2017版 spring-boot 拦截器的操作三种方式

    一.注解方式 @WebServlet(urlPatterns = "/myServlet") public class MyServlet extends HttpServlet ...

  6. 分区表主键不包含分区键报错ERROR 1105 (HY000)

    ERROR 1105 (HY000): A PRIMARY KEY must include all columns in the table's partitioning function MySQ ...

  7. HTTP 错误 500.XX - Internal Server Error 解决办法

    HTTP 错误 500.19 - Internal Server Error 无法访问请求的页面,因为该页的相关配置数据无效. 详细错误信息 模块 IIS Web Core 通知 未知 处理程序 尚未 ...

  8. 【慕课网实战】Spark Streaming实时流处理项目实战笔记四之铭文升级版

    铭文一级: 需求二:Agent选型:exec source + memory channel + logger sink# Name the components on this agenta1.so ...

  9. Django-配置、静态文件与路由

    -----配置文件 1.BASE_DIR BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))   2.DEBU ...

  10. 1033 To Fill or Not to Fill

    PAT A 1033 To Fill or Not to Fill With highways available, driving a car from Hangzhou to any other ...