当前位置:网站首页>4.泛型和工具类
4.泛型和工具类
2022-08-09 09:23:00 【过来我的小熊】
Java泛型和工具类
泛型
- 概述:Java泛型是JDK1.5中引入的一个新特性,其本质是参数化类型,把类型作为参数传递
- 常见的形式有泛型类,泛型接口,泛型方法
- 语法:<T,…> T称为类型占位符,表示一种引用类型
- 好处:可以提高代码的复用性,防止类型转换异常,提高代码的安全性
泛型类
语法:类名 T是类型占位符,表示一种引用类型,如果写多个使用逗号隔开
注意:
- 泛型只能是引用类型
- 不同泛型对象之间不能相互赋值
package com.jhkj.fx;
/**
* Generic 一般的,通用的
* 泛型类
* 语法:类名<T(类型)> T是类型占位符,表示一种引用类型,可以编写多个,如果写多个使用逗号隔开
*/
public class MyGeneric<T> {
// 使用泛型T
// 1.创建变量
T t;
// 2.作为方法参数
public void show(T t){
System.out.println(t);
}
// 3.作为返回值
public T getT(){
return t;
}
}
package com.jhkj.fx;
/**
* 使用泛型类
* 注意:
* 1.泛型只能使用引用类型
* 2.不同泛型对象之间不能相互赋值
*/
public class TestMyGeneric {
public static void main(String[] args) {
// 创建泛型类对象
MyGeneric<String> stringMyGeneric = new MyGeneric<String>();
stringMyGeneric.t = "白书";
stringMyGeneric.show("加油");
String t = stringMyGeneric.getT();
System.out.println(t);
MyGeneric<Integer> integerMyGeneric = new MyGeneric<>();
integerMyGeneric.t = 17;
integerMyGeneric.show(1);
Integer t1 = integerMyGeneric.getT();
System.out.println(t1);
}
}
泛型接口
语法:接口名 T为类型占位符,表示一种引用类型
注意:不能创建泛型静态常量
泛型接口的实现类可以为普通类,也可以为泛型类
泛型接口
package com.jhkj.fx;
public class MyInterfaceImpl implements MyInterface<String>{
@Override
public String service(String s) {
System.out.println(s);
return s;
}
}
实现类 (普通类)
package com.jhkj.fx;
public class MyInterfaceImpl implements MyInterface<String>{ // 直接确定类型
@Override
public String service(String s) {
System.out.println(s);
return s;
}
}
实现类(泛型类)
package com.jhkj.fx;
public class MyInterfaceImpl2<T> implements MyInterface<T> { // 在给泛型类传递泛型参数的同时实现接口
@Override
public T service(T t) {
System.out.println(t);
return t;
}
}
Test
package com.jhkj.fx;
public class Test {
public static void main(String[] args) {
MyInterfaceImpl myInterface = new MyInterfaceImpl();
myInterface.service("白书");
MyInterfaceImpl2<Integer> impl2 = new MyInterfaceImpl2<>();
impl2.service(17);
}
}
泛型方法
语法:修饰符 返回值类型 方法名(T t)
package com.jhkj.fx;
/**
* 泛型方法
* 语法: 修饰符 <T> 返回值类型 方法名(T t)
*/
public class MyGenericMethod {
// 泛型方法
public <T> void show(T t){
System.out.println("泛型方法" + t);
}
}
package com.jhkj.fx;
public class Test {
public static void main(String[] args) {
// 泛型方法
MyGenericMethod genericMethod = new MyGenericMethod();
genericMethod.show("白书"); // 可以为任意类型
}
}
泛型集合
- 概念:参数化类型,类型安全的集合,强制集合元素的类型必须一致
- 特点
- 编译时即可检查,而非运行时抛出异常
- 访问时,不必类型转换(拆箱)
- 不同泛型之间引用不能相互赋值,泛型不存在多态
package com.jhkj.fx;
import com.jhkj.collection.Student;
import java.util.ArrayList;
import java.util.Iterator;
/**
* 泛型集合
*/
public class Demo {
public static void main(String[] args) {
ArrayList ts = new ArrayList();
// 当不写泛型时,是可以添加任何类型元素,但是在转换类型时,还得必须确认之前类型
ts.add("你");
ts.add("我");
ts.add("他");
ts.add(10);
ts.add(20);
for (Object t : ts) {
// String s = (String) t;
System.out.println(t);
}
ArrayList<Student> students = new ArrayList<>(); // 当泛型指定之后,那输入的数据就必须是泛型类型的数据
Student s1 = new Student("张三",14);
Student s2 = new Student("李四",13);
Student s3 = new Student("王五",12);
students.add(s1);
students.add(s2);
students.add(s3);
Iterator<Student> iterator = students.iterator();
while (iterator.hasNext()){
System.out.println(iterator.next());
}
}
}
边栏推荐
猜你喜欢
随机推荐
Redis high availability
Rights management model, ACL, RBAC and ABAC (steps)
unix环境编程学习-多线程
Redis Basics
第四讲 SVN
性能测试的基本概念是什么?做好性能测试需要掌握哪些知识?
.equals ==
Jfinal loading configuration file principle
测试计划包括哪些内容?目的和意义是什么?
6.File类
TypeScript Brief (1)
中国打造国产“谷歌地球”清晰度吓人
有返回值的函数
QT sets the icon of the exe executable
可以写进简历的软件测试项目实战经验(包含电商、银行、app等)
国产谷歌地球同款软件,查看下载19级高清卫星影像so easy!
问卷问题和答案的合并
一个项目的整体测试流程有哪几个阶段?测试方法有哪些?
Anti App so层对抗分析
seata处理分布式事务