一、什么是泛型?
- 参数化类型
- 为什么要引入泛型?
1.将不同类型的数据添加到Arraylist中,取出数据要使用时,要进行强制转换(还原-向下转型)。
2.同时在编写程序时,不会报错(无类型安全监测机制)。而结果出错ClassCastException。
泛型
- 编译时安全监测机制
- 减少了强制转换操作
1.background
package com.jiang.obj3;
import java.lang.reflect.Array;
import java.util.ArrayList;
public class background {
public static void main(String[] args) {
ArrayList arrayList=new ArrayList();
arrayList.add("java");
arrayList.add(100);
arrayList.add(true);
//1.
//for (Object o : arrayList) {
// 存进去,就得取出来用,用的话就得还原本来的类型
// String str= (String) o;
// java.lang.Integer cannot be cast to java.lang.String
// 编写时不报错,但是结果报错,很危险----泛型提供编译时类型安全监测机制
// System.out.println(str);
//}
ArrayList<String> strlist =new ArrayList<>();
//strlist.add(100); 报错
//编译期间检查
//减少强制类型转换
strlist.add("a");
strlist.add("b");
for (String s : strlist) {
System.out.println(s);
}
}
}
1.1结果
"C:\Program Files\Java\jdk1.8.0_291\bin\java.exe" "-javaagent:D:\IDEA\IntelliJ IDEA 2020.3.3\lib\idea_rt.jar=63433:D:\IDEA\IntelliJ IDEA 2020.3.3\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_291\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\rt.jar;D:\IDEA_Project\XML\target\classes;C:\Users\JR2002\.m2\repository\org\dom4j\dom4j\2.1.3\dom4j-2.1.3.jar;C:\Users\JR2002\.m2\repository\jaxen\jaxen\1.2.0\jaxen-1.2.0.jar" com.jiang.obj3.background
a
b
Process finished with exit code 0
2.泛型案例ProductGetter
package com.jiang.obj3;
import java.util.ArrayList;
import java.util.Random;
public class ProductGetter<T> {
Random random=new Random();
private T product;
ArrayList<T> list =new ArrayList<>();
public void addProduct(T t) {
list.add(t);
}
//折不是泛型方法
//采用类的泛型不能用static,因为数据类型没有被确定
public T getProduct(){
product = list.get(random.nextInt(list.size()));
return product;
}
//泛型方法如下;
//右边泛型标识和也应当一样,毕竟返回值是E
public <E> E getProduct(ArrayList<E> list){
return list.get(random.nextInt(list.size()));
}
//静态的泛型方法
public static <T,E,V> void printtype(T t,E e,V v){
System.out.println(t+":"+t.getClass().getSimpleName());
System.out.println(e+":"+e.getClass().getSimpleName());
System.out.println(v+":"+v.getClass().getSimpleName());
}
//可变参数的泛型方法
public static <E> void print(E...e){
for (E e1 : e) {
System.out.println(e1);
}
}
}
2.1测试
@Test
public void TestProductGetter(){
ProductGetter<String> productGetter=new ProductGetter<>();
String[] product={"苹果手机","华为手机","笔记本","平板"};
for (String s : product) {
productGetter.addProduct(s);
}
System.out.println("您抽到的奖品为:"+productGetter.getProduct());
System.out.println("----------------------------------------");
ProductGetter<Integer> productGetter2=new ProductGetter<>();
int[] product2={1000,2000,50,10000};
for (int i : product2) {
productGetter2.addProduct(i);
}
System.out.println("你抽到的奖金为:"+productGetter2.getProduct());
}
2.2结果
"C:\Program Files\Java\jdk1.8.0_291\bin\java.exe" -ea -Didea.test.cyclic.buffer.size=1048576 "-javaagent:D:\IDEA\IntelliJ IDEA 2020.3.3\lib\idea_rt.jar=62931:D:\IDEA\IntelliJ IDEA 2020.3.3\bin" -Dfile.encoding=UTF-8 -classpath "D:\IDEA\IntelliJ IDEA 2020.3.3\lib\idea_rt.jar;D:\IDEA\IntelliJ IDEA 2020.3.3\plugins\junit\lib\junit5-rt.jar;D:\IDEA\IntelliJ IDEA 2020.3.3\plugins\junit\lib\junit-rt.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\rt.jar;D:\IDEA_Project\XML\target\test-classes;D:\IDEA_Project\XML\target\classes;C:\Users\JR2002\.m2\repository\org\dom4j\dom4j\2.1.3\dom4j-2.1.3.jar;C:\Users\JR2002\.m2\repository\junit\junit\4.12\junit-4.12.jar;C:\Users\JR2002\.m2\repository\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar;C:\Users\JR2002\.m2\repository\jaxen\jaxen\1.2.0\jaxen-1.2.0.jar" com.intellij.rt.junit.JUnitStarter -ideVersion5 -junit4 com.jiang.obj.Testobj,TestProductGetter
您抽到的奖品为:平板
----------------------------------------
你抽到的奖金为:1000
Process finished with exit code 0
二、泛型类、接口
常用泛型标识: T、E、K、V
泛型类
3.泛型类Generic02
package com.jiang.obj3;
public class Generic02<T> {
private T key;
public Generic02(T key) {
this.key = key;
}
public T getKey() {
return key;
}
@Override
public String toString() {
return "Generic02{" +
"key=" + key +
'}';
}
public void setKey(T key) {
this.key = key;
}
}
3.1 测试
Generic02<String> generic02 = new Generic02< >("a");
后面的String可以省略;Generic02 generic021 = new Generic02(100);
未指定对应的类型的时候,将按照object类型来处理Generic02 generic022=new Generic02(100);不支持基本数据类型 底层是object转化成(object继承)相应的对象,而int 无- 统一泛型,创建不同数据类型的创建的对象是同一类型
@Test
public void TestGeneric() {
Generic02<String> generic02 = new Generic02<String>("a");
//后部分的string可以省略
System.out.println(generic02.getKey());
Generic02 generic021 = new Generic02(100);
//未指定对应的类型的时候,将按照object类型来处理
System.out.println(generic021.getKey());
//Generic02<int> generic022=new Generic02<int>(100);
//不支持基本数据类型 底层是object转化成(object继承)相应的对象,而int 无
//统一泛型,创建不同数据类型的创建的对象是同一类型
System.out.println(generic02.getClass());
System.out.println(generic021.getClass());
System.out.println(generic02.getClass() == generic021.getClass());
}
3.2结果
"C:\Program Files\Java\jdk1.8.0_291\bin\java.exe" -ea -Didea.test.cyclic.buffer.size=1048576 "-javaagent:D:\IDEA\IntelliJ IDEA 2020.3.3\lib\idea_rt.jar=62583:D:\IDEA\IntelliJ IDEA 2020.3.3\bin" -Dfile.encoding=UTF-8 -classpath "D:\IDEA\IntelliJ IDEA 2020.3.3\lib\idea_rt.jar;D:\IDEA\IntelliJ IDEA 2020.3.3\plugins\junit\lib\junit5-rt.jar;D:\IDEA\IntelliJ IDEA 2020.3.3\plugins\junit\lib\junit-rt.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\rt.jar;D:\IDEA_Project\XML\target\test-classes;D:\IDEA_Project\XML\target\classes;C:\Users\JR2002\.m2\repository\org\dom4j\dom4j\2.1.3\dom4j-2.1.3.jar;C:\Users\JR2002\.m2\repository\junit\junit\4.12\junit-4.12.jar;C:\Users\JR2002\.m2\repository\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar;C:\Users\JR2002\.m2\repository\jaxen\jaxen\1.2.0\jaxen-1.2.0.jar" com.intellij.rt.junit.JUnitStarter -ideVersion5 -junit4 com.jiang.obj.Testobj,TestGeneric
a
100
class com.jiang.obj3.Generic02
class com.jiang.obj3.Generic02
true
Process finished with exit code 0
泛型的继承
- 父类Parent
package com.jiang.obj3;
public class Parent<T> {
private T key;
public T getKey() {
return key;
}
public void setKey(T key) {
this.key = key;
}
}
4.1子类是泛型 firstson
- 泛型子类标识符得一致
package com.jiang.obj3;
//泛型子类继承泛型父类
//泛型标识必须一致
public class firstson<T> extends Parent<T> {
@Override
public void setKey(T key) {
super.setKey(key);
}
}
4.2子类不是泛型 secondson
- 子类不是泛型,父类泛型必须明确类型
package com.jiang.obj3;
//子类不是泛型,父类得明确类型
public class secondson extends Parent<String>{
@Override
public void setKey(String key) {
super.setKey(key);
}
}
4.3测试
@Test
public void TestParent_firstson_secondson(){
firstson<String> firstso=new firstson<>();
firstso.setKey("first");
System.out.println(firstso.getKey());
System.out.println("----------------------");
secondson secondson=new secondson();
secondson.setKey("second");
System.out.println(secondson.getKey());
}
4.4结果
"C:\Program Files\Java\jdk1.8.0_291\bin\java.exe" -ea -Didea.test.cyclic.buffer.size=1048576 "-javaagent:D:\IDEA\IntelliJ IDEA 2020.3.3\lib\idea_rt.jar=62841:D:\IDEA\IntelliJ IDEA 2020.3.3\bin" -Dfile.encoding=UTF-8 -classpath "D:\IDEA\IntelliJ IDEA 2020.3.3\lib\idea_rt.jar;D:\IDEA\IntelliJ IDEA 2020.3.3\plugins\junit\lib\junit5-rt.jar;D:\IDEA\IntelliJ IDEA 2020.3.3\plugins\junit\lib\junit-rt.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\rt.jar;D:\IDEA_Project\XML\target\test-classes;D:\IDEA_Project\XML\target\classes;C:\Users\JR2002\.m2\repository\org\dom4j\dom4j\2.1.3\dom4j-2.1.3.jar;C:\Users\JR2002\.m2\repository\junit\junit\4.12\junit-4.12.jar;C:\Users\JR2002\.m2\repository\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar;C:\Users\JR2002\.m2\repository\jaxen\jaxen\1.2.0\jaxen-1.2.0.jar" com.intellij.rt.junit.JUnitStarter -ideVersion5 -junit4 com.jiang.obj.Testobj,TestParent_firstson_secondson
first
----------------------
second
Process finished with exit code 0
泛型接口
5.接口 _interface
package com.jiang.obj3;
public interface _interface<T> {
T getkey();
}
5.1实现类不是泛型 firstinterface
- 接口明确类型
package com.jiang.obj3;
public class firstinterface implements _interface<String>{
private String key;
@Override
public String getkey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
}
5.2实现类是泛型 secondinterface
- 实现类的必须有一个泛型标识和泛型接口一致
package com.jiang.obj3;
//必须有一个泛型标识符相同
public class secondinterface<T,E> implements _interface<T>{
private T key;
private E value;
@Override
public T getkey() {
return key;
}
public E getValue(){
return value;
}
public void setKey(T key) {
this.key = key;
}
public void setValue(E value) {
this.value = value;
}
}
5.3测试
@Test
public void TestInterface(){
firstinterface firstinterface=new firstinterface();
firstinterface.setKey("first");
System.out.println(firstinterface.getkey());
System.out.println("---------------------");
secondinterface<String,Integer> secondinterface=new secondinterface<>();
secondinterface.setKey("second");
secondinterface.setValue(2);
System.out.println(secondinterface.getkey() + ":" + secondinterface.getValue());
}
5.4结果
"C:\Program Files\Java\jdk1.8.0_291\bin\java.exe" -ea -Didea.test.cyclic.buffer.size=1048576 "-javaagent:D:\IDEA\IntelliJ IDEA 2020.3.3\lib\idea_rt.jar=62859:D:\IDEA\IntelliJ IDEA 2020.3.3\bin" -Dfile.encoding=UTF-8 -classpath "D:\IDEA\IntelliJ IDEA 2020.3.3\lib\idea_rt.jar;D:\IDEA\IntelliJ IDEA 2020.3.3\plugins\junit\lib\junit5-rt.jar;D:\IDEA\IntelliJ IDEA 2020.3.3\plugins\junit\lib\junit-rt.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\rt.jar;D:\IDEA_Project\XML\target\test-classes;D:\IDEA_Project\XML\target\classes;C:\Users\JR2002\.m2\repository\org\dom4j\dom4j\2.1.3\dom4j-2.1.3.jar;C:\Users\JR2002\.m2\repository\junit\junit\4.12\junit-4.12.jar;C:\Users\JR2002\.m2\repository\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar;C:\Users\JR2002\.m2\repository\jaxen\jaxen\1.2.0\jaxen-1.2.0.jar" com.intellij.rt.junit.JUnitStarter -ideVersion5 -junit4 com.jiang.obj.Testobj,TestInterface
first
---------------------
second:2
Process finished with exit code 0
泛型方法
泛型成员方法不是泛型方法
泛型成员方法
//这不是泛型方法
//采用类的泛型不能用static,因为数据类型没有被确定
public T getProduct(){
product = list.get(random.nextInt(list.size()));
return product;
}
6.泛型方法
//泛型方法如下;
//右边泛型标识和也应当一样,毕竟返回值是E
public <E> E getProduct(ArrayList<E> list){
return list.get(random.nextInt(list.size()));
}
多个泛型参数的泛型方法
成员方法为什么不能static?
假如能调用static成员方法,
- 类名.static成员方法。X 因为成员方法的类型未被指定,而指定的话是需要靠所在类先指定的。
- 类型.static方法。 此时同样是未被指定,但是它能够自己指定。
(因为泛型类需要在创建对象时指定类型,而静态方法是通过类名直接调用的,此时成员方法类型未被指定,会报错。)
//静态的泛型方法
public static <T,E,V> void printtype(T t,E e,V v){
System.out.println(t+":"+t.getClass().getSimpleName());
System.out.println(e+":"+e.getClass().getSimpleName());
System.out.println(v+":"+v.getClass().getSimpleName());
}
可变参数的泛型方法
//可变参数的泛型方法
public static <E> void print(E...e){
for (E e1 : e) {
System.out.println(e1);
}
}
整体
package com.jiang.obj3;
import java.util.ArrayList;
import java.util.Random;
public class ProductGetter<T> {
Random random=new Random();
private T product;
ArrayList<T> list =new ArrayList<>();
public void addProduct(T t) {
list.add(t);
}
//这不是泛型方法
//采用类的泛型不能用static,因为数据类型没有被确定
public T getProduct(){
product = list.get(random.nextInt(list.size()));
return product;
}
//泛型方法如下;
//右边泛型标识和也应当一样,毕竟返回值是E
public <E> E getProduct(ArrayList<E> list){
return list.get(random.nextInt(list.size()));
}
//静态的泛型方法
public static <T,E,V> void printtype(T t,E e,V v){
System.out.println(t+":"+t.getClass().getSimpleName());
System.out.println(e+":"+e.getClass().getSimpleName());
System.out.println(v+":"+v.getClass().getSimpleName());
}
//可变参数的泛型方法
public static <E> void print(E...e){
for (E e1 : e) {
System.out.println(e1);
}
}
}
6.1 测试
@Test
public void Testmethond(){
ProductGetter<String> productGetter=new ProductGetter<>();
String[] product={"苹果手机","华为手机","笔记本","平板"};
for (String s : product) {
productGetter.addProduct(s);
}
//成员方法
System.out.println(productGetter.getProduct());
System.out.println("--------------------------------------------");
ArrayList<String> list=new ArrayList<>();
list.add("苹果手机");
list.add("华为手机");
list.add("笔记本电脑");
list.add("平板");
System.out.println("您获得的奖品是:"+productGetter.getProduct(list)+"("+productGetter.getProduct(list).getClass().getSimpleName()+")");
ProductGetter.printtype(100,"java",true);
ProductGetter.print(1,2,3,4,56);
ProductGetter.print("A","B","C");
}
6.2结果
"C:\Program Files\Java\jdk1.8.0_291\bin\java.exe" -ea -Didea.test.cyclic.buffer.size=1048576 "-javaagent:D:\IDEA\IntelliJ IDEA 2020.3.3\lib\idea_rt.jar=63333:D:\IDEA\IntelliJ IDEA 2020.3.3\bin" -Dfile.encoding=UTF-8 -classpath "D:\IDEA\IntelliJ IDEA 2020.3.3\lib\idea_rt.jar;D:\IDEA\IntelliJ IDEA 2020.3.3\plugins\junit\lib\junit5-rt.jar;D:\IDEA\IntelliJ IDEA 2020.3.3\plugins\junit\lib\junit-rt.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\rt.jar;D:\IDEA_Project\XML\target\test-classes;D:\IDEA_Project\XML\target\classes;C:\Users\JR2002\.m2\repository\org\dom4j\dom4j\2.1.3\dom4j-2.1.3.jar;C:\Users\JR2002\.m2\repository\junit\junit\4.12\junit-4.12.jar;C:\Users\JR2002\.m2\repository\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar;C:\Users\JR2002\.m2\repository\jaxen\jaxen\1.2.0\jaxen-1.2.0.jar" com.intellij.rt.junit.JUnitStarter -ideVersion5 -junit4 com.jiang.obj.Testobj,Testmethond
苹果手机
--------------------------------------------
您获得的奖品是:笔记本电脑(String)
100:Integer
java:String
true:Boolean
1
2
3
4
56
A
B
C
Process finished with exit code 0