在之前的文章我们介绍了一下 Java 中的  集合框架中的Collection 的迭代器 Iterator,本章我们来看一下 Java 集合框架中的Collection 的泛型。

在讲泛型之前我们先来看下面一段代码:

 public class Main {
public static void main(String[] args) {
Point point = new Point(1, 2); point.setX(2);
int ix = point.getX();
System.out.println(ix); // (2, 2) /**
* 如果想要 x 值变为 double 类型则可以强转为 double 类型
* */
point.setX(2);
double dx = (double) point.getX();
System.out.println(dx); // 2.0
}
} class Point {
private int x;
private int y; public Point(int x, int y) {
this.x = x;
this.y = y;
} public int getX() {
return x;
} public void setX(int x) {
this.x = x;
} public int getY() {
return y;
} public void setY(int y) {
this.y = y;
} @Override
public String toString() {
return "(" + x + ", " + y + ")";
}
}

上面的代码我们之前的文章讲过,我们可以通过传入 x 和 y 值来定义 Point 点,如果我们想要 double 类型的点时需要造型为 double 类型,那我要定义汉字类型的呢?那就造型成 String 类型,这就很麻烦,每次都需要自己来造型,有种鞋不合脚的感觉,那能不能定义我想要什么类型就是什么类型呢,如下:

 public class Main {
public static void main(String[] args) {
Point<Integer> point1 = new Point<Integer>(1, 2); // 必须是包装类
point1.setX(1);
System.out.println(point1.getX()); // Point<Double> point2 = new Point<Double>(1.1, 2.1); // 必须是包装类
point2.setX(1.2);
System.out.println(point2.getX()); // 1.2 Point<String> point3 = new Point<String>("一", "二"); // 必须是包装类
point3.setX("三");
System.out.println(point3.getX()); // 三
}
} /**
* 泛型
* 又称参数化类型,是将当前类的属性的类型,方法参数的类型及方法
* 返回值的类型的定义权移交给使用者,
* 使用者在创建当前类的同时将泛型的试剂类型传入
* 数字和字母组合,数字不能开头
*/
class Point<T> { // 定义为泛型 T 类型
private T x;
private T y; public Point(T x, T y) {
this.x = x;
this.y = y;
} public T getX() {
return x;
} public void setX(T x) {
this.x = x;
} public T getY() {
return y;
} public void setY(T y) {
this.y = y;
} @Override
public String toString() {
return "(" + x + ", " + y + ")";
}
}

从上面的代码中,我们定义了一个 T 的类型 Point,当我们要实例化该类时,根据自己的需求传入想要的包装类类型即可,这样就满足了不同的需求,各取所需。

泛型从底层来说其实就是 Object,定义了泛型只是编译器在做一些验证工作,当我们对泛型类型设置值时,会检查是否满足类型要求,当我们获取一个泛型类型的值时,会自动进行类型转换。

在平时我们是很少自己来定义泛型的,泛型是用来约束集合中元素的类型,如下:

 import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator; public class Main {
public static void main(String[] args) {
Collection<String> collection = new ArrayList<String>(); // 只能添加 String 类型的元素
collection.add("one");
collection.add("two");
collection.add("thee");
collection.add("four");
// collection.add(1); // 编译错误
for (String string : collection) {
System.out.println(string); // one two three four
}
Iterator<String> iterator = collection.iterator();
while (iterator.hasNext()) {
// String string = (String) iterator.next(); 不需要再造型
String string = iterator.next();
System.out.println(string); // one two three four
}
}
}

最新文章

  1. [System] CentOS虚拟机系统克隆后的网络配置
  2. 【代码笔记】iOS-日历
  3. STL---deque(双端队列)
  4. angular run()运行块
  5. 给jdk写注释系列之jdk1.6容器(11)-Queue之ArrayDeque源码解析
  6. 在Servlet中使用spring注入的bean
  7. chrome提供的功能
  8. SICP 练习 (2.12)解决摘要 :不同的实现时间
  9. c# in deep 之LINQ简介(1)
  10. JVM参数设置、分析
  11. eclipse控制台中文乱码解决
  12. Oozie时bin/oozied.sh start或bin/oozied.sh run出现Bootstrap进程无法启动,http://bigdatamaster:11000/oozie界面也无法打开?E0103: Could not load service classes, java.lang.ClassNotFoundException: Class org.apache.oozie.ser
  13. BBS论坛(十九)
  14. SDOI2019 省选前模板整理
  15. linux下socket connect 阻塞方式 阻塞时间控制
  16. sql 查询表格中多列重复的数据并显示该表的其他列
  17. CENTOS 6.6初始化SHELL脚本
  18. sudo env
  19. Pytorch 0.3加载0.4模型及其之间版本的变化
  20. Codeforces Round #246 (Div. 2) D E

热门文章

  1. SQL server数据库的密码策略与登录失败锁定策略
  2. Android Studio代码编译通过但是提示停止运行
  3. Java——使用ObjectMapper.writeValueAsString时报错The type com.fasterxml.jackson.core.JsonProcessingException cannot be resolved. It is indirectly referenced from required .class files
  4. Pandas读取文件报错UnicodeDecodeError: &#39;utf-8&#39; codec can&#39;t decode byte 0xb6 in position 0: invalid start byte
  5. PowerDesigner使用教程(一)
  6. PETS渗透测试标准总结
  7. 最小生成树——Prim算法理解
  8. 轻松实现记录与撤销——C#中的Command模式
  9. Rocket - decode - SimplifyDC
  10. 【Mybatis】mybatis开启Log4j日志、增删改查操作