泛型的本质就是将数据类型也参数化, 普通方法的输入参数的值是可以变的,但是类型(比如: 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+8NWtrZ2q7y7icuVsqEwiik5Lru8W0k2Shx513ZCRKJIW6Z1RZcli5fI4SlBkklRokgRpEiCIHiAN0EQIG7w7v1hgJmenu45OANiCL5PvR+AQb+Z9/p1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tOFMAhNAhUzcAsCEwU+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/CAZlh8uQgNaCz4ZunxjbIqy1SdDlxs9RUgAwsVg8NPR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EG4ABBBuAAQQbgAEEG4ABBBuAAQQbgAEEG4ABBBuAAQQbgAEEG4ABBBuAAQQbgAEEG4ABBBuAAQQbgAEEG4ABBBuAAQQbgAEEG4ABBBuAAQQbgAEEG4ABBBuAAQQbgAEEG4ABBBuAAQQbgAEEG4ABBBuAAQQbgAEEG4ABBBuAAQQbgAEEG4ABBBuAAQQbgAEEG4ABBBuAAQQbgAEEG4ABBBuAAQQbgAEEG4ABBBuAAQQbgAEEG4ABBBuAAQQbgAEEG4ABBBuAAQQbgAEEG4ABBBuAAQQbgAEEG4ABBBuAAQQbgAEEG4ABDx/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泛型方法与泛型类的使用------------(五)的更多相关文章

  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#反射中的MakeGenericType函数,来为泛型方法和泛型类指定(泛型的)类型

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

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

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

  8. Java Spring Boot VS .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. hdu 6393 Traffic Network in Numazu (树链剖分+线段树 基环树)

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=6393 思路:n个点,n条边,也就是基环树..因为只有一个环,我们可以把这个环断开,建一个新的点n+1与之相 ...

  2. MT【50】高中曲线系集大成之双切线法

    [历史使人聪明,诗歌使人机智,数学使人精细,哲学使人深邃,道德使人严肃,逻辑与修辞使人善辩.--- Bacon,Francis] 练习: 评:这道2011高考题的解析做法参考答案也值得一看,但我这边在 ...

  3. 自学Linux Shell12.1-结构化命令if-then

    点击返回 自学Linux命令行与Shell脚本之路 12.1-结构化命令if-then 许多程序要求对shell脚本中的命令施加一些逻辑流程控制.有一类命令会根据条件使脚本跳过某些命令.这样的命令通常 ...

  4. [luogu1447][bzoj2005][NOI2010]能量采集

    题目大意 求出\(\sum_{i=1}^{n} \sum_{i=1}^{m} gcd(i,j)\times 2 -1\). 题解 解法还是非常的巧妙的,我们考虑容斥原理.我们定义\(f[i]\)表示\ ...

  5. 用powershell 批量卸载 windows 更新

    $KBID = "KB958488" $KBID1 = "KB976902" cls function Remove-Update { $HotFixes = ...

  6. Android log 方法

    package test; public abstract class Logger { private static Class<? extends Logger> mLoggerCla ...

  7. 2019.3.28&2019.3.30考试

    2019.3.28 : 肥肠爆芡,因为这场考试的题太屑了,所以我咕咕了 Upd on 2019.3.30 压进来一篇(因为都没啥意义) 2019.3.30 : 全机房读错题+没有大样例=T2全体爆炸 ...

  8. CF 1023

    昨天晚上打的一场CF,口胡一下前4题吧. A要注意细节,先找*,如果没有就判两者相等. 然后注意长度n - 1 <= m,然后前后比较,最后判断中间是不是字母. B先判断有没有解,然后求出 k ...

  9. MQTT——取消订阅报文和断开连接报文

    笔者已经把连接报文,订阅报文,发布报文都讲解了完了.而接下来就是取消订阅报文和断开连接报文.和其他的报文比较的话,他们显示非常简单.甚至笔者觉得可以不必要拿出来讲.只要看一下MQTT文档就没有什么不清 ...

  10. 2018.9青岛网络预选赛(H)

    传送门:Problem H https://www.cnblogs.com/violet-acmer/p/9664805.html 题意: BaoBao在一条有刻度的路上行走(哈哈,搞笑),范围为 [ ...