JDK 1.5 introduced new features - generics

xiaoxiao2021-03-06  19

1. Introduction The new language ingredients are introduced in JDK 1.5, generics is one of them. Simple generple 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 {void add (e x); item item ();} public interface iterator {e next (); boolean hasnext ();} Type parameters related to polar brackets It is the new thing introduced by JDK 5, which is the "form type parameter" of the List and Iterator interface (referred to as "Type Gate") declaration. And when the generic declaration list (for example: List ), All types of types (such as e), will be replaced by the "actual type parameters" (referred to as "type", such as Integer). Although the template mechanism in C is imagined in form, It is necessary to note that the generic statement in Java will never be launched in multiple copies when calling: whether it is in the source level, binary grade, or in disk or memory, it will not be expanded! The generic statement will only be Just compile 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 to "Form value parameters", similarly, generic declarations also have its "type parameters"; when the method is called, the actual parameter will replace the formal parameters, then perform the method body, the same, when the generic declaration is called When the actual type parameters replace the type of type parameters. Remarks on naming conventions: Recommended the type of type parameter in the form of refined and concise (e.g., a single character), is recommended, it is best to avoid using uppercase characters so that The parameters of the class or interface are distinguished. Many promotional goods types use e to indicate the type of type of its element. 2. generics and subtyping (generics and subtyping) first look at the following two lines of code: List ls = new arraylist (); // 1List LO = LS; // 2 The first line is no problem, the key is in the second line of code, most people will think that "a string List is naturally Is an object List, therefore, the second line is no problem. Ok, then look at the following code: lo.add (New Object ()); // 3String s = ls.get (0); // 4: Try to assign an Object to a string! Visible, through alias LO, we can perform data operations for LS, a String list ( Especially inserting an Object), leading to the LS is not just a String object! This is the Java compiler is not allowed! Compile, the second line will report a compilation error. Usually, if foo is a subtype of BAR (Sub-class or sub-interface), g is a generic statement, then G is not a subtype of g . This is often the most difficult to understand because it is from 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 assumes that all elements in a collection are output. The following is the old version and the new version (JDK 1.5) Writing: Void PrintCollection (Collection C) {Iterator i = C.ITerator (); for (k = 0; k C) {for (Object E: C) {System.out.Println (E);

}} The problem is that the new version is more useful than the old version. Because the old version can use various types of collection as a parameter, the new version can only use collection . As you can see, Collection Not a super-type (parent type) of all sets. All collection of all sets should be written: Collection , Read: Collection of unknown, a collection, the element type can be used with any type Match. Therefore, this type is "wild-column type". Correctly implement the old version of the code to write: Void PrintCollection (Collection C) {for (Object E: c) {system.out.println e);}} This method can be called with any type of collection. Note that in the method body, you still read the element from C and assign it to Object, this is not wrong, so regardless of the type What a collection, its elements are Object. However, if any gives it to it, it is unsafe: Collection C = new arraylist (); c.Add (new object ()); // Compile error Because we don't know what the element type C is, it cannot add an object. Method Add () accepts a parameter of type E, and E is the same as the elements of the collection. When the type of arguments are When it indicates "Unknown Type", the parameters we passed 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 anything Parameters. 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 return element type is "Unknown type", but it is always an Object, so the element returned to the entry to an Object type variable, or the parameters that pass it to an accessible object are safe.

转载请注明原文地址:https://www.9cbs.com/read-52841.html

New Post(0)
CopyRight © 2020 All Rights Reserved
Processed: 0.038, SQL: 9