|NO.Z.00056|——————————|BigDataEnd|——|Java&集合类库.V04|——|Java.v04|泛型机制.v04|泛型类|被集成处理方式|定义使用|
一、泛型类被继承时的处理方式
### --- [泛型类被继承时的处理方式]——[泛型方法的定义和使用]
~~~ # 泛型在继承上的体现
——> 如果B是A的一个子类或子接口,而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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· DeepSeek 开源周回顾「GitHub 热点速览」