Generic Java

generic

Generics: Tags
insert image description here

  • Collection interfaces and collection classes have been modified to have generic structures after jdk5.0
  • When instantiating a collection class, you can specify the specific generic type
  • After specifying, whenever a class or interface is defined in a collection class or collection interface, the internal structure using generics will be designated as the generic type at the time of instantiation.
  • The generic type must be a class, not a basic data type. The basic data type needs to be used and replaced with a wrapper class.
  • If the location of the generic is not specified when instantiating, it is the Object type.

package Collection;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import org.junit.Test;

public class ArrayListTest {
	// 使用泛型
	@Test
	public void test(){
		ArrayList<Integer> list = new ArrayList<Integer>();
		list.add(12);
		list.add(25);
		list.add(33);
		//list.add("AA");//报错,不让你进来了,保证数据的安全
		// 方式一
//		for(Integer scores : list){
//			int stuScore = scores;//不用强转了
//			System.out.println(stuScore);
//		}
	
		// 方式二
		Iterator<Integer> iterator = list.iterator();
		while(iterator.hasNext()){
			int scores = iterator.next();
			System.out.println(scores);
		}
		
	}
	
	//在集合中使用泛型
	@Test
	public void test2(){
		Map<String,Integer> map = new HashMap<String,Integer>();
		
		map.put("tom", 123);
		map.put("am", 52);
		map.put("sd", 13);
		map.put("twd", 173);
		
		// 泛型的嵌套
		Set<Map.Entry<String,Integer>> entry = map.entrySet();
		System.out.println(entry);
		Iterator<Map.Entry<String, Integer>> iterator = entry.iterator();
		while(iterator.hasNext()){
			Map.Entry<String, Integer> e = iterator.next();
			String key = e.getKey();
			Integer value = e.getValue();
			System.out.println(key + "----------" + value);
		}
		
		
	}

}

Custom generic structure: generic class, generic interface, generic method

A custom Order class

package File;

// 自定义泛型类
public class Order<T> {
	
	String orderName;
	int orderId;
	
	// 类的内部结构就可以使用类的泛型
	T orderT;
	public Order(){}
	
	public Order(String orderName,int orderId,T orderT){
		this.orderName = orderName;
		this.orderId = orderId;
		this.orderT = orderT;
	}
	
	public T getOrderT(){
		return orderT;
	}
	
	public void setOrderT(T orderT){
		this.orderT = orderT;
	}

	@Override
	public String toString() {
		return "Order [orderName=" + orderName + ", orderId=" + orderId + ", orderT=" + orderT + "]";
	}
	
	
}

Tested under the same package

package File;

import org.junit.Test;

public class GenericTest1 {
	
	@Test
	public void test1(){
		//Order order = new Order();  //没有指明类的泛型,则此泛型类型为Object
		// 建议写上,这样第三个参数就是String类型
		Order<String> order1 = new Order<String>("OrderAA",123,"string");
		
		
		
	}

}

When a subclass inherits a parent class with generics, it specifies the generic type. When instantiating an object, it no longer needs to specify the generic type.

Points to note

insert image description here

insert image description here

A generic method is a generic structure that appears in the method, and the generic parameters have nothing to do with the generic parameters of the class. That is, it does not matter whether the class to which the generic method belongs is a generic class.

Generic methods can be declared static because the generic parameters are determined when the method is called, not when instantiated.

The embodiment of generics in inheritance relationships

Class A is the parent class of class B. G "A" and G "B" do not have a child-parent class relationship. They are in a parallel relationship.

List<Object> list1 = null;
List<String> list2 = null;
list1 = list2;//报错

But A《G》 is the parent class of B《G》

Use of wildcards

Wildcard: ?
The common parent class of G《A》 and G《B》 is G《? "

@Test
public void test2(){
	List<Object> list1 = null;
	List<String> list2 = null;
	
	List<?> list3 = null;
	
	list3 = list1;
	list3 = list2;
	
	print(list1);//
	print(list2); //都可以调用了
}

public void print(List<?> list){
	Iterator<?> iterator = list.iterator();
	while(iterator.hasNext()){
		Object obj = iterator.next();
		System.out.println(obj);
	}
	
	}

For list<?>, you cannot add data to the list. The only way to add Null is to allow reading data. The type of reading is object.

Restricted use of wildcards

? extends Person (<=Person)
? super Person (>=Person)

For the second one, you can add (Person and subclasses of Person)

Guess you like

Origin blog.csdn.net/abc1234564546/article/details/127963065