1 Introduction
New language components are introduced in JDK 1.5, generics is one of them. Simple generic (Defining Simple Generics)
The following code is taken from the List interface of the java.util package and the definition of the Iterator interface:
Public interface list
Type parameters
Some things related to polar brackets are new things introduced by JDK 5, they are the "formal type parameters" of the List and Iterator interfaces ("Types") declaration. When the generic declaration list is called (for example : List
Although the template mechanism in C is imagined in the form, it is necessary to note that generic statements in Java will never be displayed in multiple copies when they call: whether they are in the source level, binary, or in disk or memory It will not be launched!
The generic statement will only be compiled once, and generate a class file (Class file), which is exactly the same as a normal class or interface.
Type parameters are actually similar to the usual parameters used in the method or constructor. A method can declare it "form value parameters", similarly, generic declarations also have its "type parameters"; When called, the actual parameter will replace the formal parameters, then perform the method body, the same, when the generic declaration is called, the actual type parameter replaces the type parameters of the form.
Remarks on naming conventions: Recommended types of type parameters in form using refined and concise (e.g., single characters). It is best to avoid using lowercase characters to distinguish between the parameters of ordinary classes or interfaces. Many promotional goods Types use e to indicate type of the type of its element.
2. Generals and Subtyping
First look at the following two lines of code is legal: List
Ok, then look at the following code: lo.add (new object ()); // 3String s = ls.get (0); // 4: Try to give an Object to a string! Visible, through alias LO, we can For LS, a String list, data operation (especially inserting an Object), resulting in LS not only to accommodate String objects! This is the Java compiler is not allowed! Compile, the second line will report a compilation error of.
Generally, if foo is a subtype (sub-class or sub-interface) of BAR, G is a generic statement, then G
This is often the most difficult to understand because it is and the usual intuitive phase. In an intuitive understanding, we actually assume that the collection will not change, but this is not the case in the Java language.
3. Wildcards
Assume that all elements in a collection are output. The following is the following is the WITRY:
Void PrintCollection (Collection C) {Iterator i = C.Iterator (); for (k = 0; k Void PrintCollection (Collection All types of intenses should be written: Collection >, Read: Collection of unknown, a collection, the element type can match any type. Therefore, this type is called "wildcaries". Correctly implement the old version of the code described above: Void PrintCollection (Collection > C) {for (Object E: c) {system.out.println (e);}} At this time, this method can be called with any type of collection. Note that in the method body, it is still read from the element and assigns Object, which is not wrong, so regardless of the type of arguments, it The elements are Object. However, if any gives an Object, it is unsafe: Collection > C = new arraylist Since we don't know what is the element type of C, you can't add an object. Method Add () accepts a type E parameter, and E is the same as the elements type of the collection. When the type of arguments are, it means "it" Unknown type ", the parameters we pass to add must be this" unknown type "subtype. Unfortunately, since the type is unknown, you can't determine its child type, so you can't use it as a parameter. The only exception is Null, because null is a member of all types. On the other hand, if you give a list <>> we can call the get () method and use the elements returned. Although the type of elements returned is "unknown type", it is always an Object, so it will be Get () The returned element is assigned to an Object type variable, or the parameters that pass it to a acceptable object are safe. {TO BE Continued}