🔥作者主页:小林同学的学习笔录
🔥mysql专栏:小林同学的专栏
目录
1.泛型的认识
2.泛型类
3.泛型方法
4.泛型接口
5.特别强调
6.类型通配符
简单化知识点,让你彻底了解泛型
1.泛型的认识
Java泛型是一种在Java编程语言中用来创建泛型类、接口和方法的机制。泛型提供了在编译时期
进行类型检查和类型安全的方法,使得程序员可以在编写代码时指定类、接口或方法可以操作的数
据类型,而不必在实际使用时再指定类型。
使用泛型的好处包括:
- 类型安全:泛型可以在编译时检查类型,避免了在运行时出现类型转换错误。
- 代码重用:泛型可以使代码更加通用,可以用于多种类型的数据。
- 简化代码:泛型可以减少代码的重复量,提高代码的可读性和可维护性。
- Get获取数据不需要强转类型
java 中泛型标记符:
- E - Element (在集合中使用,因为集合中存放的是元素)
- T - Type(Java 类)
- K - Key(键)
- V - Value(值)
- N - Number(数值类型)
- ? - 表示不确定的 java 类型
下面代码将包括泛型类,泛型方法,泛型接口进行说明
// 定义一个泛型类
class Box<T> {private T content;// 构造函数public Box(T content) {this.content = content;}// 获取盒子中的内容public T getContent() {return content;}// 设置盒子中的内容public void setContent(T content) {this.content = content;}
}// 定义一个泛型方法
class Util {// 泛型方法示例:交换数组中两个元素的位置// 注意:// 1.只有声明了<T>的方法才是泛型方法,泛型类中的使用了泛型的成员方法并不是泛型方法。// 2.<T>表明该方法将使用泛型类型T,此时才可以在方法中使用泛型类型T。public static <T> void swap(T[] array, int i, int j) {T temp = array[i];array[i] = array[j];array[j] = temp;}
}// 定义一个泛型接口
interface Pair<K, V> {K getKey();V getValue();
}// 实现泛型接口
class OrderedPair<K, V> implements Pair<K, V> {private K key;private V value;public OrderedPair(K key, V value) {this.key = key;this.value = value;}public K getKey() {return key;}public V getValue() {return value;}
}public class Main {public static void main(String[] args) {// 使用泛型类Box<Integer> integerBox = new Box<>(10);System.out.println("Box contains: " + integerBox.getContent());// 使用泛型方法String[] names = {"Alice", "Bob"};Util.swap(names, 0, 1);System.out.println("Swapped names: " + names[0] + " and " + names[1]);// 使用泛型接口Pair<String, Integer> pair = new OrderedPair<>("one", 1);System.out.println("Key: " + pair.getKey() + ", Value: " + pair.getValue());}
}
2.泛型类
泛型类定义相对比较简单
泛型类,是在实例化类的时候指明泛型的具体类型
// 定义一个泛型类
class Box<T> {private T content;// 构造函数public Box(T content) {this.content = content;}// 获取盒子中的内容public T getContent() {return content;}// 设置盒子中的内容public void setContent(T content) {this.content = content;}
}
3.泛型方法
这里需要注意的是,很多人分不清泛型方法和使用泛型方法
泛型方法一定有<T>定义泛型,然后在参数列表使用泛型
使用泛型方法是通过拿类或者接口所定义的泛型来使用
// 定义一个泛型方法
class Util {// 泛型方法示例:交换数组中两个元素的位置// 注意:// 1.只有声明了<T>的方法才是泛型方法,泛型类中的使用了泛型的成员方法并不是泛型方法。// 2.<T>表明该方法将使用泛型类型T,此时才可以在方法中使用泛型类型T。public static <T> void swap(T[] array, int i, int j) {T temp = array[i];array[i] = array[j];array[j] = temp;}
}
4.泛型接口
泛型接口和泛型类定义基本一致
// 定义一个泛型接口
interface Pair<K, V> {K getKey();V getValue();
}
5.特别强调
- 1.只有声明了<T>的方法才是泛型方法,泛型类中的使用了泛型的成员方法并不是泛型方法。
- 2.<T>表明该方法将使用泛型类型T,此时才可以在方法中使用泛型类型T。
- 3.注意泛型类型参数只能代表引用型类型,不能是原始类型(像 int、double、char 等)
- 4.静态方法无法访问类上定义的泛型;如果静态方法操作的引用数据类型不确定的时候,必须要将泛型定义在方法上。
public class Model<T> {......../*** 如果在类中定义使用泛型的静态方法,需要添加额外的泛型声明(将这个方法定义成泛型方法)* 即使静态方法要使用泛型类中已经声明过的泛型也不可以。* 如:public static void demo(T t){..},此时编译器会提示错误信息:"StaticGenerator cannot be refrenced from static context"*/public static <T> void demo(T t){}
}
6.类型通配符
类型通配符一般是使用 ? 代替具体的类型参数。例如 List<?> 在逻辑
是 List<String>,List<Integer> 等所有 List<具体类型实参> 的父类。
import java.util.*;public class GenericTest {public static void main(String[] args) {List<String> name = new ArrayList<String>();List<Integer> age = new ArrayList<Integer>();List<Number> number = new ArrayList<Number>();name.add("icon");age.add(18);number.add(314);getData(name);getData(age);getData(number);}public static void getData(List<?> data) {System.out.println("data :" + data.get(0));}
}