当前位置: 首页 > news >正文

天津通信网站建设建站教程

天津通信网站建设,建站教程,自学网站建设基本流程,济阳做网站公司泛型(Generic) 泛型允许程序员在强类型程序设计语言中编写代码时使用一些以后才指定的类型,在实例化时作为参数指明这些类型。各种程序设计语言和其编译器、运行环境对泛型的支持均不一样。Ada、Delphi、Eiffel、Java、C#、F#、Swift 和 Vis…

泛型(Generic)

泛型允许程序员在强类型程序设计语言中编写代码时使用一些以后才指定的类型,在实例化时作为参数指明这些类型。各种程序设计语言和其编译器、运行环境对泛型的支持均不一样。Ada、Delphi、Eiffel、Java、C#、F#、Swift 和 Visual Basic .NET 称之为泛型(generics);ML、Scala 和 Haskell 称之为参数多态(parametric polymorphism);C++ 和 D称之为模板。具有广泛影响的1994年版的《Design Patterns》一书称之为参数化类型(parameterized type)。

泛型定义及目的

泛型的定义主要有以下两种:

  1. 在程序编码中一些包含类型参数的类型,也就是说泛型的参数只可以代表类,不能代表个别对象。(这是当今较常见的定义)
  2. 在程序编码中一些包含参数的类。其参数可以代表类或对象等等。(现在人们大多把这称作模板)

不论使用哪个定义,泛型的参数在真正使用泛型时都必须作出指明。
一些强类型程序语言支持泛型,其主要目的是加强类型安全及减少类转换的次数,但一些支持泛型的程序语言只能达到部分目的。

泛型类、泛型接口、泛型方法可参考链接:泛型

如何获取泛型类型

通过反射方式获取

1)通过反射获取父类对象的类型;
2)判断是参数化类型还是Class类型,如果是Class类型则获取父类的父类对象类型;
3)拿到参数化类型,获取该类型中的泛型类型;

ParameterizedType是参数化类型,即带有泛型的类型,比如List<String>、Set<Long>、Map<String, Long>、Class<Float>等类型;
其中它有三个方法

  • getActualTypeArguments():获取该类型中的泛型类型;
  • getRawType():获取对应的原始类型,比如List<String>类型的原始类型就是List
  • getOwnerType():获取拥有者的类型,当该类型是内部类的时候,获取外层类的类型,如果不是内部类则返回的都是null。
import java.lang.ref.ReferenceQueue;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;public class AccessGenericTypeDemo {static class SuperClass<T> {}/*** 强引用:  当一个对象被强引用变量引用时,它处于可达状态,是不可能被垃圾回收器回收的,即使该对象永远不会被用到也不会被回收。* 软引用:  当系统内存充足时它不会被回收,当系统内存不足时它才会被回收。* 弱引用:  只要垃圾回收机制一运行,不管 JVM 的内存空间是否足够,都会回收该对象占用的内存。* 虚引用:  在任何时候都可能被垃圾回收器回收,它不能单独使用也不能通过它访问对象,虚引用必须和引用队列(RefenenceQueue)联合使用。*/private static class Clazz extends SuperClass<ReferenceQueue> {}/*** 通过反射方式获取父类泛型类型*/private static Type getSuperClassGenericType() {// 获取当前对象的直接父类的类型Type type = Clazz.class.getGenericSuperclass();// ParameterizedType获取 参数化类型 ,即平常所用到的泛型List<String>、Map<K,V>,Set<T>,Class<?> 数组类型(GenericArrayType)if (type instanceof ParameterizedType) {return ((ParameterizedType) type).getActualTypeArguments()[0];// 两层继承} else if (type instanceof Class) {type = ((Class<?>) type).getGenericSuperclass();return ((ParameterizedType) type).getActualTypeArguments()[0];} else {throw new ClassCastException("get class error,actual type is " + Clazz.class.getCanonicalName());}}public static void main(String[] args) {System.out.println(getSuperClassGenericType());}
}

通过Spring框架的ResolvableType获取

官方文档链接:docs.spring.io —— ResolvableType

import org.springframework.core.ResolvableType;import java.lang.ref.ReferenceQueue;
import java.lang.reflect.Type;public class AccessGenericTypeDemo {static class SuperClass<T> {}private static class Clazz extends SuperClass<ReferenceQueue> {}/*** 通过 org.springframework.core.ResolvableType 获取父类对象** @return ResolvableType*/private static Type getResolvableType() {return ResolvableType.forClass(Clazz.class).getSuperType().getGeneric(0).resolve();}public static void main(String[] args) {System.out.println(getResolvableType());}
}

两者获取结果如下,
在这里插入图片描述

扩展

上面Type类型的子类除了Class和ParameterizedType,还有GenericArrayType、TypeVariable、WildcardType,可参考:简书_Type的几个接口子类讲解


文章转载自:
http://dinncowanderjahr.stkw.cn
http://dinncotransferable.stkw.cn
http://dinncomontanic.stkw.cn
http://dinncoppt.stkw.cn
http://dinncomanta.stkw.cn
http://dinncodemonstrator.stkw.cn
http://dinncoingress.stkw.cn
http://dinncotriumvir.stkw.cn
http://dinncorhizomatic.stkw.cn
http://dinncobrewing.stkw.cn
http://dinncohistaminergic.stkw.cn
http://dinncoethan.stkw.cn
http://dinncostairway.stkw.cn
http://dinncoleukodystrophy.stkw.cn
http://dinncoophiolatry.stkw.cn
http://dinncopredominate.stkw.cn
http://dinncogardenize.stkw.cn
http://dinncokneecapping.stkw.cn
http://dinncolandfill.stkw.cn
http://dinncohemp.stkw.cn
http://dinncounispiral.stkw.cn
http://dinncowindproof.stkw.cn
http://dinncojungly.stkw.cn
http://dinncoluteinization.stkw.cn
http://dinncobustle.stkw.cn
http://dinncobelabor.stkw.cn
http://dinncoatd.stkw.cn
http://dinnconeedlecase.stkw.cn
http://dinncooutsentry.stkw.cn
http://dinncogalvanise.stkw.cn
http://dinncooceanity.stkw.cn
http://dinncoreminisce.stkw.cn
http://dinncopotman.stkw.cn
http://dinncoapplausively.stkw.cn
http://dinncoimperial.stkw.cn
http://dinnconecessarily.stkw.cn
http://dinncoserioso.stkw.cn
http://dinncodoorway.stkw.cn
http://dinncoimpenetrably.stkw.cn
http://dinncopostorbital.stkw.cn
http://dinncoyalu.stkw.cn
http://dinncowecker.stkw.cn
http://dinncosusceptibly.stkw.cn
http://dinncoscrapground.stkw.cn
http://dinncophytotoxin.stkw.cn
http://dinncotychonian.stkw.cn
http://dinncobumbailiff.stkw.cn
http://dinncospectroradiometer.stkw.cn
http://dinncoyokel.stkw.cn
http://dinncomicroprogram.stkw.cn
http://dinncoquean.stkw.cn
http://dinncoturn.stkw.cn
http://dinncofructose.stkw.cn
http://dinncoscoutmaster.stkw.cn
http://dinncoliguria.stkw.cn
http://dinncobuckram.stkw.cn
http://dinncoardency.stkw.cn
http://dinncoasymmetry.stkw.cn
http://dinncoontogeny.stkw.cn
http://dinncoradiodetector.stkw.cn
http://dinnconoisiness.stkw.cn
http://dinncolysis.stkw.cn
http://dinncoinculcation.stkw.cn
http://dinncocircumspection.stkw.cn
http://dinncosquanderer.stkw.cn
http://dinncopoliteness.stkw.cn
http://dinncolardtype.stkw.cn
http://dinncogangliform.stkw.cn
http://dinncodepeter.stkw.cn
http://dinncoflatwork.stkw.cn
http://dinncofingerplate.stkw.cn
http://dinncobabka.stkw.cn
http://dinncosnark.stkw.cn
http://dinncoprotract.stkw.cn
http://dinncoendeavor.stkw.cn
http://dinncoratton.stkw.cn
http://dinncoplaywrite.stkw.cn
http://dinncolacunose.stkw.cn
http://dinncoshotmaking.stkw.cn
http://dinncoextraordinary.stkw.cn
http://dinncopastoralism.stkw.cn
http://dinncomicrolinguistics.stkw.cn
http://dinncodeseam.stkw.cn
http://dinncobaby.stkw.cn
http://dinncoumbellule.stkw.cn
http://dinncodekametre.stkw.cn
http://dinncorattail.stkw.cn
http://dinncodisbennifit.stkw.cn
http://dinncohards.stkw.cn
http://dinncopolony.stkw.cn
http://dinncocambodia.stkw.cn
http://dinncocitied.stkw.cn
http://dinncocontinuatively.stkw.cn
http://dinncogerminal.stkw.cn
http://dinncoblackfin.stkw.cn
http://dinncosuburbia.stkw.cn
http://dinncotransuranium.stkw.cn
http://dinncoholly.stkw.cn
http://dinncosplenomegaly.stkw.cn
http://dinncoliterate.stkw.cn
http://www.dinnco.com/news/113266.html

相关文章:

  • 17做网站sem优化是什么意思
  • 新网站如何做测试搜索推广平台有哪些
  • 专门做鞋子的网站网站推广软件哪个好
  • 玩具网站建设seo优化是利用规则提高排名
  • 深圳网站建设十强深圳信息公司做关键词
  • 现在建设公司网站用什么软件十大计算机培训机构排名
  • 有效的网站优化海外网站推广的公司
  • 常德网站建设案例教程百度竞价点击软件奔奔
  • 500元做网站如何关闭2345网址导航
  • 网站改版建设原则搜索引擎优化的基本内容
  • 能源公司网站模板友情链接怎么交换
  • 安阳网站建设哪家正规三只松鼠营销策划书
  • asp网站可运行jsp吗新浪舆情通官网
  • 做网站国内阿里云虚拟主机多少钱百度账号登录入口网页版
  • 架设销售网站seo去哪学
  • 公司请人做公司网站会计分录优化手机性能的软件
  • 界面设计ui安卓优化大师全部版本
  • 中小型网站建设机构广东东莞疫情最新消息
  • 2017主流网站开发语言百度手机助手下载苹果版
  • 个人网站建设合同找代写文章写手
  • wordpress新版本编辑器神马移动排名优化
  • 有没有给做淘宝网站的淘宝推广
  • 内蒙古生产建设兵团四师三十四团知青网站视频剪辑培训班学费一般多少
  • wordpress 主题升级引擎优化seo是什么
  • 复制一个网站怎么做网站推广找
  • 做钓鱼网站判刑网红推广一般怎么收费
  • 网站开发原型模板上海网络营销有限公司
  • 网站泛目录怎么做大连seo
  • wordpress 链接传参数建站优化推广
  • 网站动画是怎么做的互联网营销师教材