Generics Types generic learning notes 1
Author: ice clouds icecloud (AT) sina.comBLOG: http://icecloud.51.net
Time: 2004.02.15
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
Java 1.5 provides generic support. A few days ago, Sun released a TUTORIAL. The following is a learning note on the tutorial.
http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf
Generics in the Java Programming Language
Gilad Bracha
Febrary 16, 2004.
1 After the generic compilation, it will actually produce different types declarations.
Public interface list
Basic call
List
Note 1: Each generic statement is only compiled once and generates a separate file for each different type. Just like the old class. For example, there may be list
Original: a Generic Type Declaration IS Compiled Once and for ALL, AND TURNED INTO A Single Class File, Just Like An Ordinary Class or Interface Declaration.
After compiling, it is equivalent to the following class.
Public interface integerList {void add (integer x); item
2 List
The following statement will not be able to compile, an error occurs on line 2, because the LO is not the parent type of the LS.
List
Note 2: In general, if foo is a subtype of BAR, G is a generic type, then G
Original: IN General, IF Foo Is A Subtype (Subclass Or Subinterface) of Bar, And G Is Som generic Type Declaration, IT IS Not The Case That G
A generic method for collection is as follows, where the FOR cycle uses a new method:
Void PrintCollection (Collection > c) {for (Object E: c) System.out.Println (E);
Among them, Collection > Indicates that it can be any type. Such as Collection
Note 3: Collection > Is all kinds of subclasses. Instead of Collection
Original: What is the supertype of all kinds of collections It's written Collection (pronounced "collection of unknown"), that is, a collection whose element type matches anything It's called a wildcard type for obvious reason? >..
4 Bounded Wildcards. Amountable wildcard type
Unfortunately, the following methods cannot be successful. Although each object in List
Public void Drawall (List
You can solve it through Bounded Wildcard: however, this will bring a little price. That is, it is read-only, you can't add new elements to the list.
Public void Drawall (List extends Shape> Shapes) {// The following call is wrong, you can only read Shapes.Add (0, new circle ());}
Note 4: Extends Class> is a limit for wildcard type, which can accept all
Original: Extends Shape>... We have replaced the type List
This is about one-third, on the subway, and I haven't seen it.