|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

 

 

posted on 2022-04-04 15:04  yanqi_vip  阅读(29)  评论(0)    收藏  举报

导航