Generics Types generic learning note <two>

zhaozj2021-02-11  255

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:

static void fromservationtocollection (t [] a, collect c) {for (t o: a) {c.add (o); // Correct}} collection co = new arraylist Collection CS = New ArrayList ; 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

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