Basic learning of java: generics

Posted by Paul Ferrie on Thu, 19 Dec 2019 17:05:40 +0100

Concept: generics are parameterized types, widely used types

effect:

1. Security: check type security when compiling; 2. Worry free: all casts are automatic and implicit, improving code reuse rate

 

1, Generic classes: using generics when declaring
Letters:
T Type indicates type
K V represents Key Value in part-time job
E for Element
Use is to determine the type
Note:
1. Generics can only use reference types, not basic types
2. Letters cannot be used on static property | static method when generic declaration

 

Generic letters in interfaces can only be used in methods, not in global constants

 

Generic method < > before return type
You can only access the information of the object, and you cannot modify the information (because the type is uncertain)

 

2, Abstract class inheritance

package com.kennosaur.gen03;
/**
 * Parent class is generic
 * 1.attribute
 * 2.Method
 * 
 * Either erase at the same time, or the subclass is greater than or equal to the type of the parent,
 * Cannot erase subclass, parent generic
 * 1.Property type
 *   In the parent class, it depends on the parent class
 *   Subclass, depending on subclass
 * 2.Method override
 *   Depending on the parent
 * @author Administrator
 *
 * @param <T>
 */
public abstract class Father<T,T1> {
	T name;
	public abstract void test(T t);
}

/**
 * Subclass specifies specific type
 * Property type is concrete type
 * Method homology
 */
class Child1 extends Father<String,Integer>{
	String t1;
	@Override
	public void test(String t) {
		t1 = this.name;		
	}	
}
/**
 * The subclass is a generic class, and the type is determined when it is used
 *
 *
 */
class Child2<T1,T> extends Father<T,T1>{
	T t2;
	@Override
	public void test(T t) {
				
	}	
}

/**
 * The subclass is generic, and the parent class does not specify the type --- generic erasure, replace with Object
 */
class Child3<T1, T2> extends Father{

	@Override
	public void test(Object t) {
				
	}	
}

/**
 * Erasing children and parents at the same time
 */
class Child4 extends Father{

	@Override
	public void test(Object t) {
				
	}	
}

/**
 * Error: subclass erase, parent generic
 */
//class Child5 extends Father<T, T1>{
//
//	@Override
//	public void test(T t) {
//		
//	}
//
//}

3, Interface generics

package com.kennosaur.gen03;
/**
 * Generic interface: same as inheritance
 * Override method depends on parent class
 * @author Administrator
 *
 * @param <T>
 */
public interface Comparable<T> {
	void compare(T t);
}
//Declaration subclass specifies concrete type
class Comp implements Comparable<Integer>{

	@Override
	public void compare(Integer t) {
		// TODO Auto-generated method stub
		
	}
	
}
//Erase
class Comp1 implements Comparable{

	@Override
	public void compare(Object t) {
		// TODO Auto-generated method stub
		
	}
	
}

//Parent class erasure, child class generic
class Comp2<T> implements Comparable{

	@Override
	public void compare(Object t) {
		// TODO Auto-generated method stub
		
	}
	
}

//Subclass generic > = parent generic
class Comp3<T> implements Comparable<T>{

	@Override
	public void compare(T t) {
		// TODO Auto-generated method stub
		
	}
	
}

//Parent generic, child erasure -- error
//class Comp4 implements Comparable<T>{
//	
//}

 

4, Erasure of generics

1. The type is not specified when inheriting the | implementation declaration;

2. Do not specify type when using

Unified Object treatment

 

5, Generics are not polymorphic

Wildcard?

package com.kennosaur.gen04;

import com.kennosaur.gen01.App;

/**
 * ?Use of: declaration type | declaration method, cannot declare class or use when
 * ? extends: <=Upper limit, specify type as subclass or itself
 * ? super :  >=Lower limit, specify type as itself or parent
 * @author Administrator
 *
 */
public class Student<T> {
	 T score;
	 
	 public static void main(String[] args) {
		 Student<?> stu = new Student<String>();
		 test(new Student<Integer>());
		 test2(new Student<Apple>());
		 //Test3 (new student < Apple > ()); error, generics not polymorphic
		 
		 //Test4 (new student < Apple > ()); X error
		 
//		 stu = new Student<Fruit>();
//		 Test4 (stu); X error
		 
		 test4(new Student<Object>());
		 test4(new Student<Fruit>());
	 }
	 
	 public static void test(Student<?> stu) {
		 
	 }
	 
	 public static void test2(Student<? extends Fruit> stu) {
			 
	 }
	 public static void test3(Student<Fruit> stu) {
		 
	 }
	 
	 public static void test4(Student<? super Fruit> stu) {
		 
	 }
}

Nesting of generics: from the outside to the inside, one layer splitting

 

Generic arrays do not exist

Cannot create a generic array without a generic array

Can only be declared, can use "? Declaration, no practical significance

 

 

 

Topics: Attribute