当前位置:网站首页>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
- 米斗APP逆向分析
- 3.练习Thread
- 第三方免费开放API 获取用户IP 并查询其地理位置
- 高清卫星地图浏览器
- Domestic Google earth, terrain analysis seconds kill with the map software
- 常用功能测试的检查点与用例设计思路
- Onnx - environment build 】 【 tensorrt
- 软件测试面试中,面试官问你一些比较“刁难”的问题你会怎么回答
- MySQL Leak Detection and Filling (2) Sorting and Retrieval, Filtering Data, Fuzzy Query, Regular Expression
猜你喜欢

Ontology Development Diary 03-Understanding Code

使用Protege4和CO-ODE工具构建OWL本体的实用指南-1.3版本(7.4 Annotation Properties-注释属性)

HD Satellite Map Browser

谷歌地图时代结束,怎么看高清卫星影像地图?

性能测试报告包括哪些内容?模板范文哪里找?看这里
软件测试面试常见问题及答案(发散思维、接口、性能、概念、)

教你如何免费获取0.1米高精度卫星地图

条件和递归

unittest测试框架原理及测试流程解析,看完绝对有提升

QT sets the icon of the exe executable
随机推荐
Rights management model, ACL, RBAC and ABAC (steps)
JS-常用方法整理
MySQL查漏补缺(四)存储过程和游标
Teach you how to get a 0.1-meter high-precision satellite map for free
中国打造国产“谷歌地球”清晰度吓人
第四讲 SVN
MySQL indexes
软件测试分析流程及输出项包括哪些内容?
6.File类
Do you know the basic process and use case design method of interface testing?
Another implementation of lateral view explode
Ontology Development Diary 01-Jena Configuration Environment Variables
map去重代码实现
Ontology Development Diary 03-Understanding Code
软件测试的流程规范有哪些?具体要怎么做?
MySQL Leak Detection and Filling (3) Calculated Fields
Web请求原理
白盒测试的概念、目的是什么?及主要方法有哪些?
可以写进简历的软件测试项目实战经验(包含电商、银行、app等)
Consolidation of Questionnaire Questions and Answers