JAVA basics-generics, the use of generics (defined on classes, methods and interfaces), the role of wildcards

First, the origin of generics is to use

The origin of generics The origin of
generics: Introduced through the Object transformation problem The
early Object types can receive any object type, but in actual use, there will be
problems with type conversion. There is also this hidden danger, so Java provides generics to solve this security problem.

The use
of generics defines generics on classes.
Format: public class 类名<泛型类型1,…>
Note: Generic types must be reference types.
When using classes to create objects, specify generics. So what is passed in is what generic

public class Tool<Q> {
    
    
	private Q q;

	public Q getObj() {
    
    
		return q;
	}

	public void setObj(Q q) {
    
    
		this.q = q;
	}	
	public void show(Q q) {
    
    
		
		System.out.println(q);
	}
}
public static void main(String[] args) {
    
    
		Tool<String> tool =new Tool<String>(); 			
//创建工具类对象
		tool.show("abc");
	}

The result is printed out: abc

Define generics on methods.
Define format:public <泛型类型> 返回类型 方法名(泛型类型 变量名)

public class Tool<Q> {
    
    
	private Q q;

	public Q getObj() {
    
    
		return q;
	}

	public void setObj(Q q) {
    
    
		this.q = q;
	}	//注意此处区别,参数括号里面为T
	public<T> void show(T q) {
    
    			
//方法泛型最好与类的泛型一致
		System.out.println(q);			
//如果不一致,需要在方法上声明该泛型
	}
}
Tool<String> tool =new Tool<String>(); 			
//创建工具类对象
		//tool.show("abc");
		tool.show(true);

The print result is true.

tips: If the method here is changed to a static method, the return value indicates an error value. Static methods are loaded as the class is loaded. Static methods must declare their own generics. That is, public
static void Printable(W w). The generic type in the class is assigned when the object is created, and static is assigned when the static method (W
w) is called.

Define the generic on the interface.
Define the format:public interface 接口名<泛型类型>

Method 1: (recommended)

interface Inter<T>{
    
    
	public void show(T t);
}
//当我们规定接口泛型为String的时候,那么我们的方法也是String
class Demo implements Inter<String>{
    
    
	@Override
	public void show(String t) {
    
    
		System.out.println(t);
	}
}

Method 2:
//This method is mainly to implement functions, there is no need to add generics to your own class. In this way, we need to specify a generic when creating an object.

class Demo<T> implements Inter<T>{
    
    
	@Override
	public void show(T t) {
    
    
		System.out.println(t);
	}
}

2. Generic advanced wildcard

Generic wildcard: <?>
any type, if not clear, then it is Object and any Java class

When doing development, I am not sure what type to accept later, and a method may be called later to return a collection or object, and the data type is uncertain. Can we have one in front at this time? Represents any type

When the generic type on the right is uncertain, the left can be specified as?

List<?> list =new ArrayList<String>();
  • ? extends E
    downward limit, E and its subclasses? Represents the child class E represents the parent class
ArrayList<Person> list1 =new ArrayList<Person>();
			list1.add(new Person("张三", 12));
			list1.add(new Person("李四", 13));
			list1.add(new Person("王五", 14));
//这样可以放进去的原因时,Student必须是继承Person才能放进去。
//此时Student往上提升,调用Person类中的toString方法
			ArrayList<Student> list2 =new ArrayList<Student>();
			list2.add(new Student("赵六", 15));
			list2.add(new Student("周七", 16));
			list1.addAll(list2);
			System.out.println(list1);

The effect is as follows:
Insert picture description here

  • ? super E
    is limited to the upper limit, and the method of E and its parent class
    will be introduced in the TreeSet collection

Guess you like

Origin blog.csdn.net/Mr_GYF/article/details/108678245