Useage of Generic in Java

ops/2025/3/18 19:10:05/

the usage of generic in class

java">// 
public class A <T>{     //without specify a parent class or implemented interface as the super bound for generic typeT n;public void printstr(){System.out.println(n);}A(T a){this.n=a;System.out.println("A构建成功 "+a.getClass().getSimpleName());}public static void main(String[] args) {A<String> a=new A("hellow world");}
}
//specify Class A or implemented interface as the super bound for gerceric type T
public class B <T extends A>{   //”T extends B & C“ you can use & operator to specify multiple interface when bounding a generic type parameter. If there is a parent,ther parent class must put the first.T a;B(T a){this.a = a;}public void A_method(){a.printstr();}public static void main(String[] args) {B b=new B(new A("testb"));b.A_method();}
}

the usage of generic in method

java">public class Main {public static <T> void Obj_Print(T a){   //in gerneric method,the type declaration must be placed before the return typeSystem.out.println(a.getClass());}public static void main(String[] args) {   Obj_Print("Hello World");}
}

now ,suppose i need create a function,which’s method is printing the elements’type of list,weather the list contains any type;

maybe you think of the fowlling methods,however it's fault

java">    public static void main(String[] args) {List<String> list = new ArrayList<String>();list.add("A");listPrint(list);}public static <T> void listPrint(List<Objects> list){System.out.println(list.getClass());}

the reason of the erro?

Although Object is super class of String,but List<Objects> isn’t the super class of List<String>

the proper way: use wild card

java">    public static void main(String[] args) {List<String> list = new ArrayList<String>();list.add("A");listPrint(list);}public static <T> void listPrint(List<?> list){System.out.println(list.getClass());}

finally: the opposite usage of “extends” is “super”; specify the subclass or implemented interface as low bound for generic


http://www.ppmy.cn/ops/166846.html

相关文章

java使用(Preference、Properties、XML、JSON)实现处理(读写)配置信息或者用户首选项的方式的代码示例和表格对比

在Java应用程序中&#xff0c;处理应用首选项&#xff08;preferences&#xff09;有多种方法&#xff0c;包括使用java.util.prefs.Preferences类、属性文件&#xff08;如.properties文件&#xff09;、XML文件和JSON文件。下面是每种方法的详细说明和代码示例&#xff0c;最…

python-数据结构汇总,树图、代码讲解(字符串、数组、字典、集合、元组)

一、字符串&#xff08;str&#xff09; 1.增操作 1.1‘’符号拼接字符串 str1"I love"; str2"you"; #直接使用‘’符号拼接字符串变成一个字符串 str str1str2 print(str) #打印出 I love you 1.2join()方法&#xff0c;可以将列表、元组中的字符串合…

DeepSeek 3FS集群化部署临时笔记

DeepSeek 3FS集群化部署临时笔记 一、3FS集群化部署1、环境介绍2、对应的软件包安装3、编译4、部署4.1 部署monitor_collector_mainStep 2: Admin clientStep 3: Mgmtd serviceStep 4: Meta serviceStep 5: Storage serviceStep 6: Create admin user, storage targets and cha…

Linux编辑器

Linux编辑器 yum yum是包管理器,类似于Linux的应用商店,安装和卸载工具需要使用yum,Linux系统中是预装了yum的 rzsz工具: Linux和window的文件互传工具 yum install lrzsz 查看所有的软件列表: yum list el代表centos,base代表软件的提供方是base 卸载软件 yum remove l…

Mybatis批量操作

1、批量插入 <!--批量操作-插入--><!-- 相当于INSERT INTO t_goods (c1,c2,c3) VALUES (a1,a2,a3),(b1,b2,b3),(d1,d2,d3),...--><insert id"batchInsert" parameterType"java.util.List">INSERT INTO t_goods (title,sub_title,origina…

数据库系统原理|课程回顾与习题部分参考答案

YI时间&#xff5c;松子茶碎碎念&#xff5c;MM-DFW&#xff5c;LAMBDA系列 星标&#x1f31f;松子茶 更新不掉队&#x1f31f; 作者 | 松子茶 © 原创内容(除图片) 未经作者授权&#xff0c;严禁转载或镜像 一、课程简介 《数据库系统原理》课程是计算机科学与技术专…

信奥赛CSP-J复赛集训(模拟算法专题)(18):P8318 『JROI-4』淘气的猴子

信奥赛CSP-J复赛集训(模拟算法专题)(18):P8318 『JROI-4』淘气的猴子 题目描述 jockbutt 有一个正整数序列,长度为 n n n,分别为 a 1 , a 2 . . . a n a_1,a_2...a_n a1​,a2​...an​,她非常喜欢这个序列,平时都非常爱惜它们。 可是有一天,当 jockbutt 在和你约…

maven之自定义插件

写在前面 在使用maven肯定是离不开插件的&#xff0c;比如执行mvn clean或者时mvn compile其实运行的就是绑定的默认插件。虽然我们一般不需要来自定义插件&#xff0c;但是为了使用的过程中更加的清晰&#xff0c;来尝试自定义插件还是很有必要的&#xff0c;所以本文就一起来…