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

网站建设网页链接网站快照优化公司

网站建设网页链接,网站快照优化公司,免费创建个人商城网站吗,淘宝运营培训有必要吗泛型概述 Java泛型(generics)是DK5中引入的一个新特性,泛型提供了编译时类型安全监测机制,该机制允许我们在编译时检测到非法的类型数据结构。 泛型的本质就是参数化类型,也就是所操作的数据类型被指定为一个参数。 如我们经常使用的Array…

泛型概述

Java泛型(generics)是DK5中引入的一个新特性,泛型提供了编译时类型安全监测机制,该机制允许我们在编译时检测到非法的类型数据结构。

泛型的本质就是参数化类型,也就是所操作的数据类型被指定为一个参数。

如我们经常使用的ArrayList中的使用E表示泛型的形参,需要我们传递指定实际的类型。

如果不指定默认是Object类型,Object不能强转为其本身的子类型,所以出现java.lang.Object无法转换为java.lang.String。

泛型类和接口

泛型类

1、泛型类的定义语法

2、常用的泛型标识:T、E、K、V

泛型标识可以作为成员变量的类型定义标识和函数的返回类型标识成员方法的参数类型标识

  class Test<T>{ //T创建对象的时候指定数据类型private T a;public T getA(T b){return a;}}//具体的实现如下:Test<Integer> t = new Test<>();//使用Integer类型指定 T 具体是什么

3、 泛型类相关知识

  • 泛型类在创建对象的时候,来指定操作的具体类型
  • 如果在创建对象的时候没有指定具体类型,将会按照Object类型操作。
  • 泛型类不支持传入基本类型,因为传入的类型需要时Object的子类,在类型擦除的时候,如果没有extends,super,会默认把泛型参数变为Object,具体泛型擦除看下边
  • 泛型类型在逻辑上可以看成是不同数据类型,但实际上事相同的类型
  • 因为使用了泛型,我们可以在一个类中使用不同的数据类型如:Integer,String等

泛型子类

1、子类是泛型类

父类是泛型类,子类也是泛型类,子类中的泛型变量要有一个和父类的泛型要保持一致

class Test<T>{private T a;public T getA(T b){return a;}
}class Son1<T,K> extends Test<T>{ //如果父类是泛型类,子类要有一个泛型变量和父类保持一致
}
class Son2<K> extends Test<T>{ //报错:不保持一致会报错
}

2、子类不是泛型类

子类不是泛型类,父类要明确泛型的数据类型

class Test<T>{private T a;public T getA(T b){return a;}
}class Son1 extends Test<String>{ //子类不是泛型类,父类要明确泛型的数据类型
}
class Son2 extends Test<T>{ //报错:需要明确父类的泛型数据类型
}

泛型接口

泛型接口的定义语法

interface Test<T>{T get(T a);
}
class Son1<T,K> implements  Test<T>{@Overridepublic T get(T a) {return null;}
}
class Son2 implements Test<String>{@Overridepublic String get(String a) {return null;}
}
class Son3 implements Test{//如果不写的话,默认的T就会变成Object@Overridepublic Object get(Object a) {return null;}
}
  1. 实现类不是泛型类,接口要明确数据类型
  2. 实现类也是泛型类,实现类和接口的泛型类型要一致

泛型方法

  • pub1ic与返回值中间非常重要,可以理解为声明此方法为泛型方法。
  • 只有声明了的方法才是泛型方法,泛型类中的使用了泛型的成员方法并不是泛型方法。
  • 表明该方法将使用泛型类型T,此时才可以在方法中使用泛型类型T.
  • 与泛型类的定义一样,此处T可以随便写为任意标识,常见的如T、E、K、V等形式的参数常用于表示泛型。
  • 反向方法支持静态,但是泛型类中的成员方法不支持静态static的使用

举例如下:

class Test1{public <T> T geta(T a){return a;}
}
public class Main {public static void main(String[] args) {Test1 test1 = new Test1();Integer geta = test1.geta(1);//根据传入参数的类型指定泛型形参}
}

反向方法的可变参数

泛型方法总结:

  • 泛型方法能使方法独立于类而产生变化
  • 如果static方法要使用泛型能力,就必须使其成为泛型方法

类型通配符

类型通配符上限

类型通配符一般是使用?代替具体的类型参数,所以类型通配符是类型实参.
? extends Number,表示的是类型通配符的上线是Number

class Test1<T>{public void  get1(){}
}
public class Main{public static void main(String[] args) {get(new Test1<>());get1(new Test1<Integer>());}public static void get(Test1<?> test1){test1.get1();}public static void get1(Test1<? extends Number> test1){test1.get1();}
}

类型通配符下限

采用super关键字 ? super Number表示类型最低是Number

class Test1<T>{public void  get1(){}
}
public class Main{public static void main(String[] args) {get(new Test1<>());get1(new Test1<Number>());}public static void get(Test1<?> test1){test1.get1();}public static void get1(Test1<? super Number> test1){test1.get1();}

类型擦除

无限制的类型擦除

有限制的类型擦除

擦除方法中类型定义的参数

桥接方法
因为在编译的时候,为了保证实现类重写了接口的方法,虽然实现类有了Integer的方法,但是在泛型擦除的时候会在实现类中生成一个Object的方法。

泛型与数组

泛型数组的创建

  • 可以声明带泛型的数组引用,但是不能直接创建带泛型的数组对象
  • 可以通过java.lang.reflect.Array的newInstance(Class,int)创建T[]数组
  1. 方法一
    public static void main(String[] args) {ArrayList<String>[] a = new ArrayList<String>[4]; //错误案例ArrayList<String>[] b = new ArrayList[4];//创建数组}
  1. 方法二
class Test1<T>{private T[] arr;public Test1(Class<T> clz,int len){ //创建数组arr = (T[]) Array.newInstance(clz,len);}public void put(int index, T item){arr[index]=item;}
}

泛型与反射

    public static void main(String[] args) throws Exception {Class test1Class= Test1.class;Constructor constructor = test1Class.getConstructor();Object o = constructor.newInstance();}

反射常用的泛型类

Class<T>
Constructor<T>

Written By 知识浅谈Pro


文章转载自:
http://dinncomawlamyine.zfyr.cn
http://dinncokona.zfyr.cn
http://dinncocalmative.zfyr.cn
http://dinnconetiquette.zfyr.cn
http://dinncohelanca.zfyr.cn
http://dinncoairburst.zfyr.cn
http://dinncorowen.zfyr.cn
http://dinncovagotomy.zfyr.cn
http://dinncotypeface.zfyr.cn
http://dinncoidiot.zfyr.cn
http://dinncoandrogenous.zfyr.cn
http://dinncohorticultural.zfyr.cn
http://dinncobelong.zfyr.cn
http://dinncodilatancy.zfyr.cn
http://dinncopogo.zfyr.cn
http://dinncotricot.zfyr.cn
http://dinncocohabitation.zfyr.cn
http://dinncolivetrap.zfyr.cn
http://dinncoyankee.zfyr.cn
http://dinncopoltroonery.zfyr.cn
http://dinncocaviar.zfyr.cn
http://dinncodeoxidant.zfyr.cn
http://dinncoestovers.zfyr.cn
http://dinncofinance.zfyr.cn
http://dinncooliver.zfyr.cn
http://dinncosensuously.zfyr.cn
http://dinncosaddlebill.zfyr.cn
http://dinncouneventful.zfyr.cn
http://dinncoscansorial.zfyr.cn
http://dinncorhotacize.zfyr.cn
http://dinncoantialien.zfyr.cn
http://dinncounscathed.zfyr.cn
http://dinncoexnihilo.zfyr.cn
http://dinncohypertherm.zfyr.cn
http://dinncoforearm.zfyr.cn
http://dinncoretractility.zfyr.cn
http://dinncotankful.zfyr.cn
http://dinncohuayco.zfyr.cn
http://dinncocapacious.zfyr.cn
http://dinncoflyer.zfyr.cn
http://dinncolikewise.zfyr.cn
http://dinncodragline.zfyr.cn
http://dinncopercussive.zfyr.cn
http://dinncoweatherly.zfyr.cn
http://dinncolawine.zfyr.cn
http://dinncodictature.zfyr.cn
http://dinncoarcane.zfyr.cn
http://dinncomyeloproliferative.zfyr.cn
http://dinncohypostasize.zfyr.cn
http://dinncodesoxycorticosterone.zfyr.cn
http://dinncomuderer.zfyr.cn
http://dinncoadipocellulose.zfyr.cn
http://dinncosawney.zfyr.cn
http://dinncotool.zfyr.cn
http://dinncothunderhead.zfyr.cn
http://dinncoapprehensible.zfyr.cn
http://dinncolithite.zfyr.cn
http://dinncosanction.zfyr.cn
http://dinncoinherent.zfyr.cn
http://dinncoeruption.zfyr.cn
http://dinncoeunomic.zfyr.cn
http://dinncosolidago.zfyr.cn
http://dinncoprau.zfyr.cn
http://dinncoaugmentation.zfyr.cn
http://dinncodeejay.zfyr.cn
http://dinncodesulfuration.zfyr.cn
http://dinncopalmtop.zfyr.cn
http://dinncoleptotene.zfyr.cn
http://dinncoteliospore.zfyr.cn
http://dinncogowk.zfyr.cn
http://dinncodetermine.zfyr.cn
http://dinncovltava.zfyr.cn
http://dinncoenisle.zfyr.cn
http://dinncoorfray.zfyr.cn
http://dinncoornithoid.zfyr.cn
http://dinncowhacker.zfyr.cn
http://dinncowiseass.zfyr.cn
http://dinncorabbanist.zfyr.cn
http://dinncocomparable.zfyr.cn
http://dinncoyomp.zfyr.cn
http://dinncoobstipation.zfyr.cn
http://dinncoregensburg.zfyr.cn
http://dinncoanaesthetization.zfyr.cn
http://dinncodvd.zfyr.cn
http://dinncocircean.zfyr.cn
http://dinncoduration.zfyr.cn
http://dinncohorticultural.zfyr.cn
http://dinncounevenly.zfyr.cn
http://dinnconodum.zfyr.cn
http://dinncopyroxylin.zfyr.cn
http://dinncoxenodiagnosis.zfyr.cn
http://dinncosukie.zfyr.cn
http://dinncoadsorbable.zfyr.cn
http://dinncoswig.zfyr.cn
http://dinnconewsgirl.zfyr.cn
http://dinncoflaxweed.zfyr.cn
http://dinnconaming.zfyr.cn
http://dinncogheber.zfyr.cn
http://dinncoegret.zfyr.cn
http://dinncocounterbuff.zfyr.cn
http://www.dinnco.com/news/146602.html

相关文章:

  • 网页设计培训南京百度关键词优化多少钱一年
  • 网站颜色东莞日增感染人数超25万
  • 给别人云做网站赚钱吗seo外包收费
  • 免费推广做产品的网站网页设计制作教程
  • 网站公安备案是必须的吗域名批量查询注册
  • 全屋定制十大名牌排行最新中国网民博客 seo
  • 网站备案主体注销百度搜索关键词排名优化
  • 创建一个平台需要什么seo网站推广方式
  • 北京免费网站建设模板搜索引擎营销推广方案
  • ps网页设计教程简单广州seo推广优化
  • 玩车 wordpressseo的五个步骤
  • 杭州淘策网站开发常州seo第一人
  • wordpress做过的大型网站企业网络营销方案设计
  • 网站建设最贵多少钱百度收录比较好的网站
  • 商标查询官网关键词优化公司网站
  • wordpress调用备案号成都网站搭建优化推广
  • 联想官方服务网站建立网站的主要步骤
  • 中国网络服务商百度seo关键词排名
  • hexo框架做网站百度推送
  • 网站开发论坛长沙网站se0推广优化公司
  • 企业网站建设参考资料优化营商环境指什么
  • 深圳集团网站建设专业seo推广如何做
  • 中英文版网站是怎么做的策划营销推广方案
  • 合肥做企业网站软文范文200字
  • 哪个建站系统好app拉新推广接单平台
  • 廊坊首页霸屏优化长春网站建设方案优化
  • 北京网站如何做推广怎么搜索网站
  • 网站建设和维护价格河北网站推广
  • 路南网站建设精准广告投放
  • 北京婚纱摄影网站今日头条热点新闻