Rocket - util - GenericParameterizedBundle

https://mp.weixin.qq.com/s/vf0PfjbxQ3Ywjk6tk85SfA

 
介绍GenericParameterizedBundle的实现。
 
 
1. 基本介绍
 
a. Bundle:继承自Bundle,自定义的数据类型;
a. 参数化:类型化的参数为T params;
b. 通用的:cloneType返回的this.type为子类的类型;
 
2. 实现
 
覆盖了一个cloneType()方法。该方法的实现如下:
a. 获取当前对象的类:this.getClass
b. 获取第一个构造方法:.getConstructors.head
c. 调用这个构造方法,并把参数传入:.newInstance(params)
d. 类型转换:.asInstanceOf[this.type]
 
如果在这个过程中出现问题,则抛出异常:
 
3. 子类的实例
 
 
TLBundle继承自GenericParameterizedBundle:
 
 
这里调用cloneType时:
a. this是tlBundle;
b. getConstructors.head是primary constructor:
c. newInstance(params)传入的params为:TLBundleParameters.emptyBundleParams
d. 类型转换为this.type,this.type为tlBundle.type。这个this.type是随着子类类型的变化而变化的。
 
4. 附录
 
 
 
posted @ 2019-05-27 22:05  wjcdx  阅读(349)  评论(0编辑  收藏  举报