java 泛型service_java泛型

什么时java泛型?

泛型时jdk1.5版本以后出现的一种对类、方法、接口的一种类型的约束,这种类型的约束是存在与编译时期的一种约束,在程序的运行时期是没有泛型的运用。

泛型的作用只存在与代码的编译时期,运行时没有泛型的存在

泛型即"参数化类型"

就是将对象将参数传递,为了能够更好的理解泛型,我们以上一篇中的分页工具类来解释,代码如下:

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

packagecom.zs.util;importcom.zs.entity.User;importjava.util.List;public classPageUtil {private intfirstPage;private intproPage;private intcurrentPage;private intnextPage;private intlastPage;private Listlist;publicPageUtil() {

}public PageUtil(int currentPage,int countPage, Listlist) {this.firstPage = 1;this.currentPage =currentPage;this.lastPage =countPage;this.proPage = this.currentPage == 1 ? 1 : (this.currentPage - 1);this.nextPage = this.currentPage == this.lastPage ? this.currentPage : this.currentPage + 1;this.list =list;

}public intgetFirstPage() {returnfirstPage;

}public void setFirstPage(intfirstPage) {this.firstPage =firstPage;

}public intgetProPage() {returnproPage;

}public void setProPage(intproPage) {this.proPage =proPage;

}public intgetCurrentPage() {returncurrentPage;

}public void setCurrentPage(intcurrentPage) {this.currentPage =currentPage;

}public intgetNextPage() {returnnextPage;

}public void setNextPage(intnextPage) {this.nextPage =nextPage;

}public intgetLastPage() {returnlastPage;

}public void setLastPage(intlastPage) {this.lastPage =lastPage;

}public ListgetList() {returnlist;

}public void setList(Listlist) {this.list =list;

}

}

View Code

这是上一篇中我们写的分页栏的工具,当我们在前台点击了下一页等跳转按钮后,就会发送一个页数的请求到后台,后台将传递过来的页数作为当前页,然后获取当前页的数据信息,并存入一个集合中,然后通过分页类的构造方法计算上一页,下一页等信息,并将当前页的信息一并存入工具类里了。

上一篇里例子中,我们只创建了一个用户的类,只读取了user表的数据,那么如果现在有一个person表,我们要获取person表的内容,在一系列操作后获取了当前页的内容,我们要存入List类型的list中,但是我们的分页类PageUtil内的list是一个List 类型的,这样就出现了类型的不一致,但是分页的代码都是一样的,我们不能因为这个小原因,重新创建类,因此我们就会想,我们能不能将这个List内的类型当作参数传递过去呢,我们调用方法时,写的是什么类型,那么类内部的List就是什么类型。

这种将对象作为参数传递的行为就是泛型。

还是前面的pageUtil类,在没有用泛型时,现在只能传递User为类型的list,那么我们将它进行改造成泛型。

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

packagecom.zs.util;importjava.util.List;public class PageUtil{private intfirstPage;private intproPage;private intcurrentPage;private intnextPage;private intlastPage;private Listlist;publicPageUtil() {

}public PageUtil(int currentPage,int countPage, Listlist) {this.firstPage = 1;this.currentPage =currentPage;this.lastPage =countPage;this.proPage = this.currentPage == 1 ? 1 : (this.currentPage - 1);this.nextPage = this.currentPage == this.lastPage ? this.currentPage : this.currentPage + 1;this.list =list;

}public intgetFirstPage() {returnfirstPage;

}public void setFirstPage(intfirstPage) {this.firstPage =firstPage;

}public intgetProPage() {returnproPage;

}public void setProPage(intproPage) {this.proPage =proPage;

}public intgetCurrentPage() {returncurrentPage;

}public void setCurrentPage(intcurrentPage) {this.currentPage =currentPage;

}public intgetNextPage() {returnnextPage;

}public void setNextPage(intnextPage) {this.nextPage =nextPage;

}public intgetLastPage() {returnlastPage;

}public void setLastPage(intlastPage) {this.lastPage =lastPage;

}public ListgetList() {returnlist;

}public void setList(Listlist) {this.list =list;

}

}

View Code

现在这个工具类,就可以保存各种类型的数据集合了,我们可以创建多个类型来测试看能不能放进去。这里因为还要写泛型的应用,就不做测试了。

泛型的应用:

1.泛型类

packagecom.zs.service;/*** 泛型的类型类似于object类,什么类型都可以,但是不可以使用八大基本类型int、double...,

* 如果使用八大基本类型的数据,要使用包装类Integer、Double...

* 泛型定义在类上public class 类名可以定义多个泛型类型

*@param*/

public class Demo1{privateE e;publicE getE() {returne;

}public voidsetE(E e) {this.e =e;

}

}

这样就创建了一个简单的泛型类。e可以是任意类型,还可以传递多个类型:

packagecom.zs.service;importjava.util.ArrayList;importjava.util.HashMap;importjava.util.List;/*** 泛型的类型:T:type E:element K:key V:value这些都指代类型,没有区别

*@param

*@param

*@param*/

public class Demo2{private Liste;private HashMapmap;public ListgetE() {returne;

}public void setE(Liste) {this.e =e;

}public HashMapgetMap() {returnmap;

}public void setMap(HashMapmap) {this.map =map;

}

}

ce02a1ad5451e206b90e44a49f396577.png

可以看出,在创建demo的对象是,声明了list的类型为string类型,因此list只能放string类型,如果存放integer类型会报错,同样的demo2里的hashmap的数据类型为string,integer

在创建对象时,会根据实力化对象时的类型,自动填充内部的E,K,V等,按照顺序填充。

2.泛型方法

packagecom.zs.service;public classDemo3 {/*** 泛型方法的参数类型可以是任意类型的,但是在声明方法时,要在返回值前声明这是个泛型方法

*@paramt

*@param*/

public voidfun1(T t) {

System.out.println("方法执行了" +t);

}public static voidmain(String[] args) {

Demo3 demo3= newDemo3();

demo3.fun1("字符串");

demo3.fun1(111);

demo3.fun1(true);/*** 运行结果,可以看到fun内可以传任意类型参数*/}

}

3.泛型接口

packagecom.zs.service;/*** 如果一个类实现了泛型接口,那么这个类必须得到泛型

*@param*/

public class Demo4Impl implements Demo4{

@Overridepublic voidfun(T t) {

System.out.println("执行了" +t);

}public static voidmain(String[] args) {

Demo4 demo4 = new Demo4Impl<>();/*上面声明了泛型类型为string类型,因此fun只能传string类型参数*/demo4.fun("111");

}

}

泛型的作用:

1.提高了程序的安全性(泛型在集合里的使用)

2.将运行期遇到的问题,提前到了编译期

3.省去了代码强转的麻烦

4.提高了代码的重用性,实现代码的公共的封装

泛型的高级应用:

1.泛型通配符>

2. extends E>

向下限定,E及其子类

3. supper E>

向上限定,E及其父类

举例说明:

packagecom.zs.service;importjava.util.List;public classDemo5 {/*** 通配符类型传参,相当于object,可以传任意类型

*@paramlist*/

public void fun1(List>list) {

System.out.println(list);

}/*** list的类型可以是demo3或其所有子类类型的

*@paramlist*/

public void fun2(List extends Demo3>list) {

System.out.println(list);

}/*** list的类型可以是demo3或其父类类型的

*@paramlist*/

public void fun3(List super Demo3>list) {

System.out.println(list);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值