请举例说明
时间: 2024-02-20 12:00:17 浏览: 103
以下是一些Java泛型的示例:
1. 泛型类和泛型方法的定义和使用:
```java
public class GenericClass<T> {
private T value;
public T getValue() {
return value;
}
public void setValue(T value) {
this.value = value;
}
public <E> void printArray(E[] array) {
for (E element : array) {
System.out.println(element);
}
}
}
GenericClass<String> gc = new GenericClass<>();
gc.setValue("Hello");
String value = gc.getValue(); // "Hello"
Integer[] intArray = {1, 2, 3};
gc.printArray(intArray); // 输出 1 2 3
```
2. 类型通配符和边界通配符的使用:
```java
public static void printList(List<? extends Number> list) {
for (Number n : list) {
System.out.print(n + " ");
}
System.out.println();
}
List<Integer> intList = Arrays.asList(1, 2, 3);
List<Double> doubleList = Arrays.asList(1.0, 2.0, 3.0);
printList(intList); // 输出 1 2 3
printList(doubleList); // 输出 1.0 2.0 3.0
```
3. 泛型擦除和桥方法的理解和应用:
```java
public class GenericClass<T> {
public void method(T t) {
System.out.println("Generic method");
}
}
public class SubClass extends GenericClass<String> {
public void method(String s) {
System.out.println("Non-generic method");
}
}
SubClass sc = new SubClass();
sc.method("Hello"); // 输出 Non-generic method
```
4. 泛型数组的创建和使用:
```java
List<String>[] arrayOfLists = new List<String>[2]; // 编译错误
List<?>[] arrayOfLists = new List<?>[2]; // 正确的创建泛型数组的方式
arrayOfLists[0] = Arrays.asList("Hello", "World");
arrayOfLists[1] = Arrays.asList("Java", "Programming");
System.out.println(arrayOfLists[0]); // 输出 [Hello, World]
```
5. 泛型与继承的关系,类型参数的协变和逆变:
```java
public class Animal {}
public class Dog extends Animal {}
public interface MyList<E> {
void add(E e);
E get(int index);
}
public class AnimalList implements MyList<Animal> {
private List<Animal> list = new ArrayList<>();
public void add(Animal animal) {
list.add(animal);
}
public Animal get(int index) {
return list.get(index);
}
}
public class DogList implements MyList<Dog> {
private List<Dog> list = new ArrayList<>();
public void add(Dog dog) {
list.add(dog);
}
public Dog get(int index) {
return list.get(index);
}
}
MyList<Animal> animalList = new AnimalList();
animalList.add(new Animal());
animalList.add(new Dog()); // 编译错误
MyList<? extends Animal> animalList = new AnimalList();
Animal animal = animalList.get(0);
Dog dog = animalList.get(1); // 编译错误
MyList<? super Dog> dogList = new AnimalList();
dogList.add(new Animal()); // 编译错误
dogList.add(new Dog());
Animal animal = dogList.get(0); // 编译错误
```
6. 泛型和反射的关系,泛型信息的获取和使用:
```java
public class GenericClass<T> {
private T value;
public T getValue() {
return value;
}
public static void main(String[] args) throws NoSuchFieldException {
GenericClass<String> gc = new GenericClass<>();
gc.setValue("Hello");
Field field = GenericClass.class.getDeclaredField("value");
Type type = field.getGenericType();
if (type instanceof ParameterizedType) {
ParameterizedType pType = (ParameterizedType) type;
Type[] argTypes = pType.getActualTypeArguments();
for (Type argType : argTypes) {
System.out.println(argType.getTypeName()); // 输出 java.lang.String
}
}
}
}
```
7. 泛型与异常的关系,泛型异常的捕获和处理:
```java
public class MyException<T> extends Exception {
private T data;
public MyException(T data) {
this.data = data;
}
public T getData() {
return data;
}
}
try {
throw new MyException<String>("Error");
} catch (MyException<?> ex) {
System.out.println(ex.getData()); // 输出 Error
}
```
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)