; From OsteTocollection (New Integer [100], CO); // Correct fromArrayTocollection (New Number [100], CS); // Error
You can know that you don't need to pass century parameters to generic methods, the compiler will infer the type.
So, when do you use a generic method, when is the type of wildcard type? Can understand this:
Note 5: You can use generic methods to ensure the type of read and write operation. The generic method is equivalent to the original polymorphic method, and its effect is to select different parameter types when executed, and the wildcard method is equivalent to accepting the Object method.
Original: This Tells US That The Type Argument is Being Used for Polymorphism; ITS ONLY Effect Is To Allow A Variety of Actual Argument Types To Be Used At Different Invocation Sites.
Consider the following method,
Public Static Void Copy (List DEST, LIST SRC) (); Public Static Void Copy (List DEST, LIST SRC) ( ); Which one is clearer? Since S is only used once, it can be replaced with a wildcard type. This is more concise. Another advantage of wildcard types is that they can be outside the method, as a value domain, local variable (local variables) or array type. Such as
Static List > History = New ArrayList > (); Public Void Drawall (list Shapes) {history.addlast (shapes);}
To be honest, I see the above variable definitions simply want to swear. This is a list of a list, which is the list of list .
6 generic and old code
Using old code in generic code can lead to type security hazards.
Public interface inventory {void setcats; collection getcats ();} collection c = new arraylist (); c.add (new blackcat ()); Inventory.Setcats (C); Collection < CAT> k = inventory.getcats ();
Note 6: The general compiler cannot know what type of Collection references, so there is no type of Collection called Row Type. Such types indicate some unknown types, equivalent to Collection .
Original:. The compiler has no way of knowing what kind of collection the type Collection refers to It's called a raw type It's more accurate to say that the type Collection denotes a collection of some unknown type like Collection. .
In this case, put the Collection Assignment to Collection , there will be a unchecked warning, no warning is checked. Indicates that the compiler cannot guarantee its correctness. So please pay attention to unchecked warning when using the old code.
Note 7: The process of compiler is called EraSure when compiled. It can be considered to convert generic code into non-extensive versions. The final result is: JVM does not check the types of security and integrity, even if unchecked Warning is also. ERASURE will delete all generic information, such as converting list to LIST, all type variables are replaced with the top, such as Object. And regardless of the result code, the type conversion will increase. Original: Generics are implemented by the Java compilers as a front-end conversion called ensure It just like a source-to-source translation As a result, the type safety and integrity of the JVM are never at risk, even in presence of.. unchecked warnings. Erasure throws out all type information between angle brackets. For example, List is converted into List. All remaining uses of type variables are replaced by the upper bound type variable (usually Object). And, whenever the resulting code ISN't Type-Correct, a cast to the appropriate type is inserted.
Use generic code in the old code. Similar to the above situation, unchecked Warning will appear.
7 FINE PRINT Beautiful blueprint? (I don't understand the author's intention)
The generic class is shared by all its subclasses.
List l1 = new arraylist (); list l2 = new arraylist (); assert (l1.getclass () == l2.getClass ()); // Return True
Note 8: All generic classes have the same Runtime class (in fact, you can know from above), and static variables and methods are also shared between the instances of the class. This is why the reference type parameters are illegal in static methods, variables, or initialization.
Original:. All instances of generics class have the same run-time class As consequence, the static variables and methods of a class are also shared among all the instances That's why it is illegal to refer to the type parameters of a type declaration in. A Static Method or Initializer, or in the declaration or initializer of a static variable.
Another implicit fact is unable to determine if a class is instanceof. Or the type converts an object to a generic type.
Collection CS = New ArrayList ; IF (CS InstanceOf Collection ) {} // illegal (CS; // Unchecked Warning T Badcast (t, Object O) {RETURN (T) O}; // unchecked warningnote 9: Type variable does not exist at runtime! This means that generics do not take up time or space performance, but unfortunately, you can't use type conversion.
Original: Type Variables Don't Exist At Run Time. This Means That The Either No Performance Overhead In Either Time Nor Space. It Also Means That You Can't reliably use themed.
An array may not be a parameter type. Unless it is a wildcard type.
List [] lsa = new list [10]; // is not allowed = new list [10]; // Allow Object O = ISA; Object [] OA = (Object []) O ; OA [1] = new arraylist (); string s = lsa [1] .GET (); // Allowed case, Runtime Error
An array of type variables can be declared, but it cannot be initialized (New) to establish a new array. Such as NEW T [100], because the type variable does not exist.
Glossary:
Parameter Type: Parameterized Type, Shape: New ArrayList , New arraylist
Type variable: type variable, shaped like: t getcollection () ...
Wildcard Type: Wildcard Type, Shaped like: New ArrayList
Bounded wildcard type: bouded wildcard type, shaped like: new arraylist
Native type: Raw Type, refers to the use of Collection, ArrayList, etc. in the generic code.
Unchecked: Unchecked Warning, Java 1.5 Code Security Warning.
To Be Continue ...
Copyright Notice: This article is completed by ice cloud, started 9CBS, the author retains Chinese copyright. No commercial use is not permitted without permission. Welcome to the reprint, but please keep the article and copyright statement complete. For contact, please send an email: Icecloud (at) sina.com
转载请注明原文地址:https://www.9cbs.com/read-4945.html