二、泛型类和接口 之 泛型类

泛型类

泛型类的定义语法

class 类名称<泛型标识,泛型标识,...>{

  private 泛型标识 变量名; ...

}

 

 

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

(1)使用语法

类名<具体的数据类型> 对象名 = new 类名<具体的数据类型>();

(2)Java1.7以后,后面的<>中的具体的数据类型可以省略不写

类名<具体的数据类型> 对象名 = new 类名<>(); 菱形语法

泛型类注意事项:

  • 泛型类,如果没有指定具体的数据类型,此时,操作类型是Object

  • 泛型的类型参数只能是类类型,不能是基本数据类型

  • 泛型类型在逻辑上可以看成是多个不同的类型,但实际上都是相同类型

小案例:

复制代码
 1 package com.genericity;
 2 
 3 import com.sun.corba.se.impl.orbutil.ObjectStreamClassUtil_1_3;
 4 
 5 /**
 6  * 泛型类
 7  */
 8 public class MainClassTest {
 9     public static void main(String[] args) {
10         //泛型类在创建对象的时候,来指定的具体数据类型
11         Generic<String> stringGeneric = new Generic<>("abc");
12         String key1 = stringGeneric.getKey();
13         System.out.println(key1);
14 
15         System.out.println("===============================================");
16 
17         Generic<Integer> integerGeneric = new Generic<>(100);
18         String key2 = stringGeneric.getKey();
19         System.out.println(key2);
20 
21         //泛型类,不支持基本数据类型
22 //        Generic<int> integerGeneric = new Generic<>(100);
23 
24         System.out.println("===============================================");
25         //泛型类在创建对象的时候,没有指定类型,将按照Object类型来操作
26         Generic objGeneric = new Generic<>("abc");
27         Object key = objGeneric.getKey();
28         System.out.println(key);
29 
30         System.out.println("===============================================");
31         System.out.println(stringGeneric.getClass());
32         System.out.println(integerGeneric.getClass());
33         System.out.println(integerGeneric.getClass()==stringGeneric.getClass());
34     }
35 }
View Code
复制代码
复制代码
 1 运行结果:
 2 abc
 3 ===============================================
 4 abc
 5 ===============================================
 6 abc
 7 ===============================================
 8 class com.genericity.Generic
 9 class com.genericity.Generic
10 true
View Code
复制代码

抽奖小案例:

复制代码
 1 package com.genericity;
 2 
 3 import lombok.val;
 4 
 5 public class ProductGetterTest {
 6     public static void main(String[] args) {
 7         //创建抽奖对象,指定数据类型
 8         ProductGetter<String> stringProductGetter = new ProductGetter<>();
 9         String[] stringProducts={"苹果手机","华为手机","扫地机器人","咖啡机","杠铃","机械键盘"};
10         for (int i = 0; i < stringProducts.length; i++) {
11             stringProductGetter.addProduct(stringProducts[i]);
12         }
13         //开始进行抽奖
14         String stringproduct = stringProductGetter.getProduct();
15         System.out.println("恭喜您抽中了:"+stringproduct);
16 
17         System.out.println("=======================================================");
18         //假设我们的奖品是现金
19         ProductGetter<Integer> integerProductGetter = new ProductGetter<>();
20         Integer[] integerProducts={100,200,300,400,500,600};
21         for (int i = 0; i < integerProducts.length; i++) {
22             integerProductGetter.addProduct(integerProducts[i]);
23         }
24         //开始进行抽奖
25         int integerproduct = integerProductGetter.getProduct();
26         System.out.println("恭喜您抽中了:"+integerproduct);
27     }
28 }
View Code
复制代码
复制代码
1 运行结果:
2 恭喜您抽中了:扫地机器人
3 =======================================================
4 恭喜您抽中了:600
View Code
复制代码

从泛型类派生子类

子类也是泛型类,子类和父类的泛型类型要保持一致

class ChildGeneric<T> extends Generic<T>

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

class ChildGeneric extends Generic<Stirng>

小案例:

复制代码
 1 package com.genericity;
 2 
 3 /**
 4  * 创建父类 泛型类
 5  * @param <E>
 6  */
 7 public class Parent<E> {
 8     private E value;
 9 
10     public E getValue() {
11         return value;
12     }
13 
14     public void setValue(E value) {
15         this.value = value;
16     }
17 }
View Code
复制代码
复制代码
 1 package com.genericity;
 2 
 3 /**
 4  * 泛型类派生子类,子类也是泛型类,那么子类的泛型标识要和父类的一致
 5  * 当然这里子类是可以扩展泛型标识的 public class ChildFirst<T,K,V> extends Parent<T> {
 6  * @param <T>
 7  */
 8 public class ChildFirst<T> extends Parent<T> {
 9     @Override
10     public T getValue() {
11         return super.getValue();
12     }
13 }
View Code
复制代码
复制代码
 1 package com.genericity;
 2 
 3 
 4 /**
 5  * 泛型类派生子类,如果子类不是泛型类,那么父类要明确数据类型
 6  */
 7 public class ChildSecond extends Parent<Integer> {
 8     /**
 9      * 注意:如果这里 是 public class ChildSecond extends Parent {
10      * 那么子类重写父类的方法的时候,默认就是Object类型的
11      */
12 //    @Override
13 //    public Object getValue() {
14 //        return super.getValue();
15 //    }
16     @Override
17     public Integer getValue() {
18         return super.getValue();
19     }
20 
21     @Override
22     public void setValue(Integer value) {
23         super.setValue(value);
24     }
25 }
View Code
复制代码
复制代码
 1 package com.genericity;
 2 
 3 public class MainTest {
 4     public static void main(String[] args) {
 5         Parent<String> stringChildFirst = new ChildFirst<>();
 6         stringChildFirst.setValue("abc");
 7         String value = stringChildFirst.getValue();
 8         System.out.println(value);
 9 
10         System.out.println("=================================================");
11 
12         Parent<Integer> integeChildFirst = new ChildSecond();//注意:这个时候子类就是具体类型的了
13         integeChildFirst.setValue(200);
14         Integer value1 = integeChildFirst.getValue();
15         System.out.println(value1);
16     }
17 }
View Code
复制代码

 

posted on   ~码铃薯~  阅读(94)  评论(0编辑  收藏  举报

编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
历史上的今天:
2019-12-08 mybatis框架-resultMap的自动映射级别-partial 和full的探讨
2019-12-08 mybatis框架-使用resultMap实现高级结果映射,collection属性的使用
2019-12-08 mybatis框架-使用resultMap实现高级结果映射,association属性
2019-12-08 前端知识--mulline框架,返回到前一个页面,并且刷新前一个页面

导航

< 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
点击右上角即可分享
微信分享提示