Generic (java cainiao's class notes) and java class notes

Source: Internet
Author: User

Generic (java cainiao's class notes) and java class notes
1. simple Application and significance of generics 2. the upper and lower limits of generics. 3. limitations on generics and subclass inheritance 4. generic class and generic Method 5. what is Generic nesting and Generic erasure Generic (Generic)? Generic: • Java 5 began to show an extension of the Java language type, to create classes that can be parameterized by type. you can regard the type parameter as the type placeholder specified when using the parameter type, just like the formal parameter of the method is the placeholder of the actual parameter. • generics can ensure the type security and good maintainability of large applications; enable the advantages of generics: • type security, and enable the compiler to limit the judgment of generic-defined types. for example, ensure that the element types in the TreeSet must be consistent; • eliminate forced type conversion. For example, when Comparable is used, type conversion is required each time; when a generic class declares a class, an identifier is used to indicate the type of a field in the class, the return value of a method, or the type of a parameter, in this way, you only need to specify the desired type when declaring classes or making instances. Declare a class with generics: class name <generic type 1, generic type 2...> {Generic type variable name; generic type method name () {} return value type method name (generic type variable name) {}} use a generic class: class Name <specific class> Object Name = new class name <specific class> (); Note: type parameter specifications: Recommended specifications-common generic, generics are stored only in the source file, and the class file does not exist. That is to say, they will be lost during compilation, and the basic data type cannot be a generic type. K key, for example, the V value of the ing key type, for example, the E Element of the Map value type, for example, Set <E> Element indicates the Element, and the T generic type of the Element, the meaning of "Type" refers to whether "List <Object> l" and "List <String> l" can be used to run the wildcard characters for transmission during the import, otherwise, the compilation fails. Use?, Indicating an unknown type generic object: • List <?> Indicates the List set of unknown elements. • this List with wildcards only represents the parent classes of various generic lists and cannot add elements to the collection. • List <?> List = new ArrayList <> (); list. add (1); // The ERROR compiler cannot deduce public void show (List <?> based on the information type. List) {}• // indicates accepting the upper limit and lower limit of all types of List set generics. Set the upper limit of generic objects to use extends, indicates that the parameter type can only be of this type or a subclass of this type: • declared object: class name <? Extends class> object name • Definition class: class name <generic tag extends class >{} sets the lower limit of the generic object to use super, indicates that the parameter type can only be of this type or the type of parent class: • declared object: class name <? Super class> object name • Definition class: class name <generic tag extends class >{} public static void show (List <? Extends Number> l) {} public static void show (List <? Super String> l) {} public static void main (String [] args) {Person <Integer> p1 = new Person <> (); p1.setVal (99 ); person <Double> p2 = new Person <> (); p2.setVal (3.14); Person <String> p3 = new Person <> (); p3.setVal ("007 "); show (p1); // √ show (p2); // √ show (p3); // ×} public static void show (Person <? Extends Number> p) {System. out. println (p. getVal ();} public static void main (String [] args) {Person <Integer> p1 = new Person <> (); p1.setVal (99 ); // Integer Person <Double> p2 = new Person <> (); p2.setVal (3.14); // Double Person <String> p3 = new Person <> (); p3.setVal ("007"); // String Person <Object> p4 = new Person <> (); p4.setVal (new Object (); // Object show (p1 ); // × show (p2); // × show (p3); // √ show (p4); // √} Public static void show (Person <? Super String> p) {System. out. println (p. getVal ();} after the generic interface java5, you can declare the generic interface. The declaration method is the same as that of the generic class. • Public interface IDAO <T >{} there are two methods for the generic interface subclass: • Declare the generic type directly after the subclass; • Provide the specific generic public class DaoImpl <T> implements IDAO <T >{} public class DaoImpl implements IDAO <String >{} generic method in the subclass' implementation Interface generic parameters can be defined, the parameter type is the real parameter type. Format: • <generic tag> return value type method name ([generic tag parameters]...) public static <T> T show (T param) {return param ;}..... main ..... {System. out. println (show (new Date (); System. out. println (show ("cditcast "));} generic: returns a generic object by using the generic method • at this time, you must explicitly provide the parameter type Generic Array for specific types to be passed in. public <T> T [] show (T [] ts) {} public <T> void show (T... ts) {} nesting of generics can point to the generics of another class from the generics of one class: public class Demo1 {public static void main (String [] args) {Map <Str Ing, String> map = new HashMap <> (); map. put ("1", "A"); map. put ("2", "B"); map. put ("3", "C"); map. put ("4", "D"); Set <Map. entry <String, String> set = map. entrySet (); Iterator <Map. entry <String, String> it = set. iterator (); while (it. hasNext () {Map. entry <String, String> entry = it. next (); System. out. println (entry. getKey () + "-->" + entry. getValue () ;}} extensive erasure in strict generic code, classes with generic declarations should always carry type parameters. However, to be consistent with the old Java code, the type parameter can be not specified when a class with a generic declaration is used, if no type parameter is specified for this generic class, this type parameter is called an original type. By default, it is the maximum type specified when this parameter is declared; when an object with generic information is assigned to another variable without generic information, all the type information between angle brackets is thrown away. • For example, if the List <String> type is converted to List, the List checks the type of the collection elements to the upper limit of the variable, that is, the Object. Generic erasure and conversion class Num <T extends Number> {private T; public Num (t T) {this. t = t ;}.... Getter/setter ....} Public class Demo {public static void main (String [] args) {Num <Integer> n = new Num <> (5); Integer I = n. getT (); Num n2 = n; // The generic information is lost. Number num = n2.getT (); // Integer I = n2.getT ();}} public class Demo {public static void main (String [] args) {List <Integer> li = new ArrayList <> (); li. add (1); List <String> ls = null; // ls = li; List list = li; ls = list cannot be converted; // No error is reported, only warning is not checked, in this case, l ist actually references List <Integer> System. out. println ("-->" + ls. get (0); // an attempt to retrieve data as a String object }}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.