|NO.Z.00056|——————————|BigDataEnd|——|Java&集合类库.V04|——|Java.v04|泛型机制.v04|泛型类|被集成处理方式|定义使用|

一、泛型类被继承时的处理方式
### --- [泛型类被继承时的处理方式]——[泛型方法的定义和使用]

~~~     # 泛型在继承上的体现
——>        如果BA的一个子类或子接口,而G是具有泛型声明的类或接口,则G并不是G的子类型!
——>        比如:String是Object的子类,但是List并不是List的子类。
二、编程代码
package com.yanqi.task15;

//public class SubPerson extends Person { // 不保留泛型并且没有指定类型,此时Person类中的T默认为Object类型   擦除
//public class SubPerson extends Person<String> {  // 不保留泛型但指定了泛型的类型,此时Person类中的T被指定为String类型
//public class SubPerson<T> extends Person<T> { // 保留父类的泛型  可以在构造对象时来指定T的类型
public class SubPerson<T, T1> extends Person<T> { // 保留父类的泛型,同时在子类中增加新的泛型
}
三、编程代码
package com.yanqi.task15;

public class SubPersonTest {

    public static void main(String[] args) {

        // 1.声明SubPerson类型的引用指向SubPerson类型的对象并调用set方法进行测试
        //SubPerson<String> sp1 = new SubPerson();  Error: SubPerson类中不支持泛型
        SubPerson sp1 = new SubPerson();
        sp1.setGender("女");

        System.out.println("----------------------------------------");
        //SubPerson<Boolean> sp2 = new SubPerson<>();
        SubPerson<Boolean, String> sp2 = new SubPerson<>();
        sp2.setGender(true);
    }
}
四、编译打印
D:\JAVA\jdk-11.0.2\bin\java.exe "-javaagent:D:\IntelliJIDEA\IntelliJ IDEA 2019.3.3\lib\idea_rt.jar=61588:D:\IntelliJIDEA\IntelliJ IDEA 2019.3.3\bin" -Dfile.encoding=UTF-8 -classpath E:\NO.Z.10000——javaproject\NO.H.00001.javase\javase\out\production\javase com.yanqi.task15.SubPersonTest
----------------------------------------

Process finished with exit code 0

 
 
 
 
 
 
 
 
 

Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart
                                                                                                                                                   ——W.S.Landor

 

 

posted on   yanqi_vip  阅读(21)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· DeepSeek 开源周回顾「GitHub 热点速览」
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示