Java泛型方法与泛型类的使用------------(五)

时间:2022-09-17 19:28:12

  泛型的本质就是将数据类型也参数化, 普通方法的输入参数的值是可以变的,但是类型(比如: String)是不能变的,它使得了在面对不同类型的输入参数的时候我们要重载方法才行. 泛型就是将这个数据类型也搞成跟参数的值一样可以变的.

  泛型分为泛型接口,泛型类和泛型方法. 泛型接口,泛型类大家都比较熟悉了,应该都用过List, ArrayList. List就是泛型接口,ArrayList就是泛型类,我们经常看到List <E>的声明, new ArrayList<E>()的定义, 这里面的E可以是String, 也可以自己定义的类. 我感觉泛型类就JDK提供的就基本够用了,自定义使用的场景非常少了. 反而是泛型方法,对与解析自定义数据结构非常有用, 类似于toString这种场景是百试不爽.

-----------------------------------------------------------泛型方法应用实例--------------------------------------------

泛型方法不一定要在泛型类里面

定义:

1. 函数上的泛型定义

          当函数中使用了一个不明确的数据类型,那么在函数上就可以进行泛型的定义。

          public <泛型的声明> 返回值类型  函数名( 泛型 变量名  ){

          }

 例如:

package Test;

import org.junit.Test;

public class fanxing {

    /**
* 定义一个泛型方法,泛型方法也可以声明为static
*@param t1
*/
// public static <T> void testFanxing(T t1){
public <T> void testFanxing(T t1){
System.out.println(t1);
} //测试泛型类
@Test
public void test1(){
fanxing fx=new fanxing();
fx.testFanxing(2);
fx.testFanxing("字符串");
fx.testFanxing(new User("1", "QLQ"));
}
} //用于测试泛型类的类
class User{
private String id;
private String name;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
protected User(String id, String name) {
super();
this.id = id;
this.name = name;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + "]";
} }

 结果:

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAZ8AAAB6CAIAAADvfslGAAAXV0lEQVR4nO2d+XMbx5XH5++Jy5WUK7+Q8Q9bRSi7+8NWtrZ2q7y7icuVsqEwiik5Lru8W0k2Shx513ZCRKJIW6Z1RZcli5fI4SlBkklRokgRpEiCIHiA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S6F6gbYDsOnboZ24jlAaPP3OyJ8Z9SM7zgr+jtOFMAhNAhUzcAsCEwU+wKqBsAmANmil0BdQMAc8BMsSugbgBgDpgpdgXUDQDMATPFroC6AYA5YKbYFVA3ADAHzBS7AuoGAOaAmWJXtNQNDAxMjwH2A9QNDMwKA+wHW90AAAAOM6BuAAAUJ6BuAAAUJ6BuAAAUJ6BuAAAUJ6BuAAAUJ6BuAAAUJ6BuAAAUJ6BuAAAUJ6BuAAAUJ/8PzhDib5/TVv4AAAAASUVORK5CYII=" alt="" />

-----------------------------------------------------------泛型类应用实例--------------------------------------------

package Test;

import org.junit.Test;

//可以有一个,也可以有多个
public class fanxinglei<T, T2> {
// 泛型属性
private T t1; public T getT1() {
return t1;
} public void setT1(T t1) {
this.t1 = t1;
} // 泛型方法
public void testFanxing(T2 t2) {
System.out.println(t2);
System.out.println(this.getT1());
} public static void main(String[] args) {
fanxinglei<String, Integer> fanxinglei = new fanxinglei<String, Integer>();
fanxinglei.setT1("泛型类");
fanxinglei.testFanxing(4);
}
}

测试:

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAXQAAACpCAIAAAC01lFBAAAPMElEQVR4nO3d3XMU15nH8f574nIl5cqNFF9slYZscrGVra3dKu+u43Kl7FEUgoC4cHm3kmycOHh37WQUEJKXlcG4qPgFR8DaDAKzlr0hMeElGjCSLAaweNMLssRISAxYVs5e9EzP6e5zemakfjRq+H7quZg502fmdJ+e35weSeCoelwZu+NV9thpt/FugxQs+stsGwBYBc79Fy4A1gLCBYAIwgWAiOWHi5cvhAuAMMIFgIgVhYt+cQQAupWGi5svRAyAgBjCRY8YiqIot2ILF4qiKL0IF4qiRIpwoShKpAgXiqJEKrZwqet5EuTEqUHqxKnBRs8DkieGcBEaWcN9dmX85Jnhk2eGP7syTrmHotFzgiRZabgIDavhRq9OnB4YGb06Qel1emCk0TODxFhRuAiNaS0YOHfhyrUJKlwD5y40enKQDISLwbUbN8+ev3jtxk0qXGfPX2z0/CAZlh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QQbgAEEG4ABBBuAAQQbgAEEG4ABBBuAAQQbgAEEG4ABBBuAAQQbgAEEG4ABBBuAAQQbgAEEG4ABBBuAAQQbgAEEG4ABBBuAAQQbgAEEG4ABBBuAAQQbgAEEG4ABBBuAAQQbgAEEG4ABBBuAAQQbgAEEG4ABBBuAAQQbgAEEG4ABBBuAAQQbgAEEG4ABBBuAAQQbgAEEG4ABBBuAAQQbgAEEG4ABBBuAAQQbgAEEG4ABBBuAAQQbgAEEG4ABBBuAAQQbgAEEG4ABBBuAAQQbgAEEG4ABBBuAAQQbgAEEG4ABBBuAAQQbgAEEG4ABBBuAAQQbgAEEG4ABBBuAAQQbgAEEG4ABDx/6fFRiNP4Ss8AAAAAElFTkSuQmCC" alt="" />

-----------------------------------泛型接口应用实例------------

public class Demo8 {
public static void main(String[] args) {
MyInter<String> my = new MyInter<String>();
my.print("泛型"); MyInter2 my2 = new MyInter2();
my.print("只能传字符串");
}
} interface Inter<T> {
void print(T t);
} // 实现不知为何类型时可以这样定义
class MyInter<T> implements Inter<T> {
public void print(T t) {
System.out.println("myprint:" + t);
}
}
//使用接口时明确具体类型。
class MyInter2 implements Inter<String> { @Override
public void print(String t) {
System.out.println("myprint:" + t); } }

-----------------------------------泛型通配符应用实例------------------------

需求:

定义一个方法,接收一个集合对象(该集合有泛型),并打印出集合中的所有元素。

例如集合对象如下格式:

Collection<Person> coll = new ArrayList<Person>();
coll.add(new Person("jack", 20));
coll.add(new Person("rose", 18));
Collection<Object> coll2 = new ArrayList<Object>();
coll2.add(new Object());
coll2.add(new Object());
coll2.add(new Object()); Collection<String> coll3 = new ArrayList<String>();
coll3.add("abc");
coll3.add("ddd");
coll3.add("eee");

分析,集合对象中的元素的类型是变化的,方法的形参的那么泛型类型就只能定义为Object类型.

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator; public class Demo9 {
public static void main(String[] args) {
ArrayList<Object> arr = new ArrayList<Object>();
arr.add(new Object());
arr.add("String");
print(arr); //将集合的泛型设置类String类型,是Object子类
HashSet<String> hs = new HashSet<String>();
hs.add("hello");
hs.add("jack");
//由于print方法接收的集合进行了元素限定,只接受限定为Object类型的集合,编译不通过
//print(hs);
} public static void print(Collection<Object> coll) {
Iterator<Object> it = coll.iterator();
while (it.hasNext()) {
Object next = it.next();
System.out.println(next);
}
}
}

但是,由于print方法接收的集合进行了元素限定,只接受限定为Object类型的集合,编译不通过该问题如何解决?

可以把方法的形参的泛型去掉,那么方法中就把集合中的元素当做Object类型处理.

也可以使用使用泛型通配符

public class Demo9 {
public static void main(String[] args) {
ArrayList<Object> arr = new ArrayList<Object>();
arr.add(new Object());
arr.add("String");
print(arr); // 将集合的泛型设置类String类型,是Object子类
HashSet<String> hs = new HashSet<String>();
hs.add("hello");
hs.add("jack");
// 使用泛型通配符,编译通过。
print(hs);
} public static void print(Collection<?> coll) {
Iterator<?> it = coll.iterator();
while (it.hasNext()) { Object next = it.next();
System.out.println(next);
}
}
}

上述就使用了泛型通配符

通配符:?
public void show(List<?> list)
{
}
可以对类型进行限定范围。
?extends E: 接收E类型或者E的子类型。
? super E: 接收E类型或者E的父类型。

限定泛型通配符的边界

限定通配符的上边界:

extends

接收Number 类型或者Number的子类型

正确:Vector<? extends Number> x = new Vector<Integer>();
错误:Vector<? extends Number> x = new Vector<String>();

限定通配符的下边界

super

接收Integer 或者Integer的父类型

正确:Vector<? super Integer> x = new Vector<Number>();
错误:Vector<? super Integer> x = new Vector<Byte>();

总结:

  JDK5中的泛型允许程序员在编写集合代码时,就限制集合的处理类型,从而把原来程序运行时可能发生问题,转变为编译时的问题,以此提高程序的可读性和稳定

注意:

  泛型是提供给javac编译器使用的,它用于限定集合的输入类型,让编译器在源代码级别上,即挡住向集合中插入非法数据。但编译器编译完带有泛形的java程序后,生成的class文件中将不再带有泛形信息,以此使程序运行效率不受到影响,这个过程称之为“擦除”。

泛型的基本术语,以ArrayList<E>为例:<>念着typeof

  ArrayList<E>中的E称为类型参数变量

  ArrayList<Integer>中的Integer称为实际类型参数

  整个称为ArrayList<E>泛型类型

  整个ArrayList<Integer>称为参数化的类型ParameterizedType

Java泛型方法与泛型类的使用------------(五)

Java泛型方法与泛型类的使用------------(五)的更多相关文章

  1. Java 泛型方法、泛型类、通配符、通配符上下限

    泛型方法 泛型方法定义规则: 所有泛型方法声明都有一个类型参数声明部分(由尖括号分隔),该类型参数声明部分在方法返回类型之前. 每一个类型参数声明部分包含一个或多个类型参数,参数间用逗号隔开.一个泛型 ...

  2. JAVA之旅(三十五)——完结篇,终于把JAVA写完了,真感概呐!

    JAVA之旅(三十五)--完结篇,终于把JAVA写完了,真感概呐! 这篇博文只是用来水经验的,写这个系列是因为我自己的java本身也不是特别好,所以重温了一下,但是手比较痒于是就写出了这三十多篇博客了 ...

  3. 20172302 《Java软件结构与数据结构》第五周学习总结

    2018年学习总结博客总目录:第一周 第二周 第三周 第四周 第五周 教材学习内容总结 查找 查找即在某项目组中寻找某一指定目标元素,或确定该组中并不存在此元素.对其进行查找的项目组称为查找池. 1. ...

  4. 20172305 2018-2019-1 《Java软件结构与数据结构》第五周学习总结

    20172305 2018-2019-1 <Java软件结构与数据结构>第五周学习总结 教材学习内容总结 本周内容主要为书第九章内容: 查找是在某个项目组中寻找到某一指定目标元素,或者确定 ...

  5. 20172328 2018-2019《Java软件结构与数据结构》第五周学习总结

    20172328 2018-2019<Java软件结构与数据结构>第五周学习总结 概述 Generalization 本周学习了第九章:排序与查找,主要包括线性查找和二分查找算法和几种排序 ...

  6. 使用C&num;反射中的MakeGenericType函数,来为泛型方法和泛型类指定(泛型的)类型

    C#反射中的MakeGenericType函数可以用来指定泛型方法和泛型类的具体类型,方法如下面代码所示这里就不多讲了,详情看下面代码一切就清楚了: using System; using Syste ...

  7. JAVA之旅(二十五)——文件复制&comma;字符流的缓冲区,BufferedWriter,BufferedReader,通过缓冲区复制文件,readLine工作原理,自定义readLine

    JAVA之旅(二十五)--文件复制,字符流的缓冲区,BufferedWriter,BufferedReader,通过缓冲区复制文件,readLine工作原理,自定义readLine 我们继续IO上个篇 ...

  8. Java Spring Boot VS &period;NetCore (五)MyBatis vs EFCore

    Java Spring Boot VS .NetCore (一)来一个简单的 Hello World Java Spring Boot VS .NetCore (二)实现一个过滤器Filter Jav ...

  9. Java笔试面试题整理第五波

    转载至:http://blog.csdn.net/shakespeare001/article/details/51321498 作者:山代王(开心阳) 本系列整理Java相关的笔试面试知识点,其他几 ...

随机推荐

  1. 2015年12月28日 Java基础系列(六)流

    2015年12月28日 Java基础系列(六)流2015年12月28日 Java基础系列(六)流2015年12月28日 Java基础系列(六)流

  2. PDF 补丁丁 0&period;4&period;1 版将增加嵌入中文字库的功能

    有不少用户反映,部分老 PDF 文件由于在制作时没有嵌入字库,导致该文件在某些阅读器上显示为乱码.即使他们用 Acrobat 嵌入了相应的字库,文件仍然无法正确显示. 这些老 PDF 看起来具有如下相 ...

  3. USB -- BULK&lowbar;ONLY和UFI协议

    2 BULK_ONLY和UFI协议 Bulk—Only协议是USB组织针对大容量存储设备制定的一种块存储类协议,目前已经普遍应用于各种移动存储设备. USB设备分为5大类,即显示器.通信设备.音频设备 ...

  4. codeforces Gym 100187A A&period; Potion of Immortality

    A. Potion of Immortality Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/1001 ...

  5. sersync 实时同步工具

    出处:http://code.google.com/p/sersync/ 当前版本的sersync依赖于rsync进行同步.如下图所示,在同步主服务器上开启sersync,将监控路径中的文件同步到目标 ...

  6. C语言实现OOP 版本2

    写版本2的原因,还是发现在不同的具体图形模块里发现了重复的release代码,这是坏味道,所以还是决定消除这些重复代码,DRY! shape.h #ifndef SHAPE_H #define SHA ...

  7. mysql 关键字bug

    今天运行语句 select * from order; 出现bug: ERROR 1064 (42000): You have an error in your SQL syntax; check t ...

  8. UVA 10325 lottery 容斥原理

    题目链接 给出m个数, 求1-n的范围内, 无法整除这m个数之中任何一个数的数的个数. 设m个数为a[i], 对任意的i, n/a[i]是n中可以整除a[i]的数的个数, 但是这样对于有些数重复计算了 ...

  9. DB2导入导出 学习笔记

    db2pd -osinfodb2mtrk -i -d (for aix)db2 get dbm cfg show detaildb2 get db cfg show detaildb2 get sna ...

  10. ZOJ 3777-Problem Arrangement&lpar;状压DP&rpar;

    B - Problem Arrangement Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %l ...