博客
关于我
泛型Generic
阅读量:660 次
发布时间:2019-03-15

本文共 2742 字,大约阅读时间需要 9 分钟。

泛型

使用泛型:

好处:
避免了类型转换的麻烦,存储的是什么类型,取出的就是什么类型

先介绍不适用泛型存储数据

//不使用泛型:好处是简单,默认object,都可以储存,坏处:容易引起异常。如果想调用某个特定类型的方法,需要向下转型,比较麻烦!

代码:

public class Demo01Generic {public static void main(String[] args) {    show01();    //show02();}  /*private static void show02() {     ArrayList
list=new ArrayList<>(); // 只能存储字符串类型的数据!}*/private static void show01() { ArrayList list=new ArrayList(); list.add("tianshan"); list.add(true); list.add(12345); Iterator it = list.iterator(); while (it.hasNext()) { Object object = it.next(); System.out.println(object); //想要调用字符串类型的方法,使用向下转型; String s=(String)object; System.out.println(s.length());//Exception in thread "main" java.lang.ClassCastException: java.lang.Boolean cannot be cast to java.lang.String报错,不能转为string类型。 }}}

定义一个泛型元素类

代码:

其中E代表Element

public class GenericClass
{private E name;public E getName() { return name;}public void setName(E name) { this.name = name;}}//给这个对象类的属性使用泛型,getter/setter方法也是用泛型传递

测试类

代码:

public class GenericClassTest {public static void main(String[] args) {    GenericClass gc =new GenericClass();    gc.setName("只要是基本数据类型即可/或者对象数据类型也可以");    Object name = gc.getName();    System.out.println(name);//只要是基本数据类型即可/或者对象数据类型也可以    GenericClass
gcint=new GenericClass<>(); gcint.setName(1); Integer name1 = gcint.getName(); System.out.println(name1);//1,自动拆箱 GenericClass
gcstr=new GenericClass<>(); gcstr.setName("String类型"); System.out.println(gcstr.getName());//String类型}}

其中第一个是没有数据类型的数据直接导入,getname方法返回对应的是Object类型

第二个传递的是integer类型的数据,getname直接返回的是integer类型的数据,同样的string类型的也是这样!
看你传入什么类型的,获取的就是什么类型的!

定义一个泛型的接口,再分别使用非泛型的类和泛型的类实现它,最后看看两者的差别!

泛型接口

代码:

public interface GenericInterface {public void method(I i);//定义一个抽象方法;}

非泛型的类来实现(实现接口的时候已经将数据类型定义为了String类型,抽象方法的重写要保持一致性)

代码:

public class GenericInterfaceImpl implements GenericInterface
{@Overridepublic void method(String s) {//数据类型已经定义好了 System.out.println(s);}}

泛型的类来实现(实现的时候要保持元素的一致性,不可少了抽象类型的元素类型,如下面方法中的I实现的是接口中的I,实现的类可以添加元素,单数不可少了元素)

代码:

public class GenericInterfaceImpl2 implements GenericInterface{ @Overridepublic void method(I i) {    System.out.println(i);}}

//public class GenericInterfaceImpl2<String,I> implements GenericInterface也是可以的

测试类

代码:

public class GenericInterfaceTest {public static void main(String[] args) {    //方法一实现类定义了string类型了。    GenericInterfaceImpl gc1=new GenericInterfaceImpl();    gc1.method("周日补周五的课,美滋滋gepi~~~~~");    //gc1.method(1234);错误写法。定义好了数据类型    //方法二实现类跟着接口类是泛型,没有固定。    GenericInterfaceImpl2 gc2=new GenericInterfaceImpl2();    gc2.method(1234);    gc2.method("看来你是真的皮");}}

实现效果

周日补周五的课,美滋滋gepi~~~~~
1234
看来你是真的皮
实现原理参考开头的案例!

转载地址:http://mljmz.baihongyu.com/

你可能感兴趣的文章
mysql中出现Unit mysql.service could not be found 的解决方法
查看>>
mysql中出现update-alternatives: 错误: 候选项路径 /etc/mysql/mysql.cnf 不存在 dpkg: 处理软件包 mysql-server-8.0的解决方法(全)
查看>>
Mysql中各类锁的机制图文详细解析(全)
查看>>
MySQL中地理位置数据扩展geometry的使用心得
查看>>
Mysql中存储引擎简介、修改、查询、选择
查看>>
Mysql中存储过程、存储函数、自定义函数、变量、流程控制语句、光标/游标、定义条件和处理程序的使用示例
查看>>
mysql中实现rownum,对结果进行排序
查看>>
mysql中对于数据库的基本操作
查看>>
Mysql中常用函数的使用示例
查看>>
MySql中怎样使用case-when实现判断查询结果返回
查看>>
Mysql中怎样使用update更新某列的数据减去指定值
查看>>
Mysql中怎样设置指定ip远程访问连接
查看>>
mysql中数据表的基本操作很难嘛,由这个实验来带你从头走一遍
查看>>
Mysql中文乱码问题完美解决方案
查看>>
mysql中的 +号 和 CONCAT(str1,str2,...)
查看>>
Mysql中的 IFNULL 函数的详解
查看>>
mysql中的collate关键字是什么意思?
查看>>
MySql中的concat()相关函数
查看>>
mysql中的concat函数,concat_ws函数,concat_group函数之间的区别
查看>>
MySQL中的count函数
查看>>