Generics Types generic learning note
Author: ice clouds Time: 2004-02-24 Contact: icecloud (AT) sina.comBlog: http: //icecloud.51.net
First of all, I would like to thank Schlemiel, he pointed out the problem of type cord. In fact, I have also seen the text of Gigix translation, but I didn't understand it. I was reminded by Schlemiel, I realized that this is a generic specification. Turned out again, such as 醍醐 醍醐 顶, real teacher Yiyou is also.
I have written the purpose of the article is to find more to exchange. There is no communication, no atmosphere, there is absolutely no improvement. Thank you Schlemiel, every time you read his article or comment, you can be highly inspired.
If you need to understand generics, please read the programmer magazine 2003 No. 7, or
http://gigix.cool2u.net/download/variance_in_java.pdf
OK, continue the last study notes. This 22 page article just reads 3 days. Every day from the 40th of Metro, forty is from the weight gate, read 7 pages each time. If is the night 6: 30-7: 00, I saw someone in the subway to hold the computer book in reading. I would like to ask if I ask if it is ^ ___ ^ ||
Another: There are a lot of focus on each chapter in the following article, and I will follow the title of chapter in the text. NOTES number yourself.
This time, the font is changed to 14px, I don't know how you look? Will it be too big?
5 generic method (function)
According to the previous, since the wildcard type is read-only, how can you write in a function? This introduces generic mehtods:
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 SRC) (); Public Static
Static List > History = New ArrayList
> (); Public Void Drawall (list extends Shape> 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 Extends Shape>.
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
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
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
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
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
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
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:
Wildcard Type: Wildcard Type, Shaped like: New ArrayList >
Bounded wildcard type: bouded wildcard type, shaped like: new arraylist EXTENDS OBJECT>
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