--- Generic Java Collections Framework

1. Overview and basic use Generic

A: Generics Overview: It is a definite type of work to
be deferred to create an object
or when calling a method to clear before a special type.
Parameterized type, the same type as a parameter passed.
B: Generic format: <data type> where the data type can only be reference data type
C: Generic benefits
(1): the problem run time to compile time ahead
(2): to avoid the cast
(3 ): optimized programming to solve the yellow warning line
Note: only generics at compile-effective but at runtime it erased

2. The origin of generics

Origin generics: Object Transformation introduced through
early type of Object may receive any type of object, but in actual use, there will be problems type conversion.
This risk also exists, it provides a generic Java to solve this security problem.

3. Overview and use of generic classes

A: Overview generic classes: the generic class defined in, and generic methods as part of a generic class type parameter declaration may also contain one or more types of parameters, the parameters are separated by commas. A generic parameter, also referred to as a variable type is used to specify the name of a generic type identifier. Because they accept one or more parameters, these classes are called parameterized classes or parameterized type.
B: Definition Format: public class class name <generic type 1, ...>
C: Note: The generic type must be a reference type
D: Case presentation: Using generic class of
generic generic classes of applications: is to generic class defined in the
format: public class class name <data type ...> {}

4. Summary and use generic methods

A: Generic Method Overview: The generic method defined in the method may receive different types of parameters in the call. The parameters passed to the method of the generic type, the compiler properly processed each method call.
B: Definition Format: public <generic type> return type names (variable names generic type)

5. Summary and use generic interface

A: Overview Generic Interface: The interface is defined in the generic
B: Definition Format: public interface interface name <generic type>

6. Superior of generic wildcard

A: Generic wildcard <?>: Any type, if inconclusive, arbitrary and Object is a Java class
B :? extends E: defining downwardly, E and its subclasses
C :? super E: defining upwardly, E and parent class
D: case presentation: basic use of advanced generics wildcards

/**
	 * 泛型如果明确了数据类型以后,那么要求左右两边的数据类型必须一致
	 */
	Collection<Object> col1 = new ArrayList<Object>() ;
	Collection<Object> col2 = new ArrayList<Animal>() ;//报错

	// ? 表示任意的数据类型
	Collection<?> col5 = new ArrayList<Object>() ;
	Collection<?> col6 = new ArrayList<Animal>() ;

	// ? extends E : 向下限定	, ? 表示的是E或者E的子类
	//		Collection<? extends Animal> col9 = new ArrayList<Object>() ;//报错
		Collection<? extends Animal> col10 = new ArrayList<Animal>() ;
		Collection<? extends Animal> col11 = new ArrayList<Dog>() ;
			// ? super E:  向上限定 , ? 表示的是E或者E的父类
	Collection<? super Animal> col13 = new ArrayList<Object>() ;
	Collection<? super Animal> col14 = new ArrayList<Animal>() ;
	//		Collection<? super Animal> col15 = new ArrayList<Dog>() ;//报错

“The sun himself is weak when he first rises, and gathers strength and courage as the day gets on.”
— Charles Dickens, Writer

Published 39 original articles · won praise 1 · views 568

Guess you like

Origin blog.csdn.net/love_to_share/article/details/102931215