J2SE5.0 norm programming
晁 晁 攀 Smallnest@163.com
This chapter is mainly referred to the Sun company documentation.
C programmers are definitely unfamiliar with the model, especially when STL is very difficult, C # 2.0 will also achieve the function of the model programming. Java also does not have to show weakness, and also introduces the new language characteristics of the model programming.
1. A simple model example
Before, you may have encountered this code:
List list = new linkedList (); list.add ("MAS)); List.add (" Princeton "); List.Add (" Berkeley); string name = (String) list.iterator.next () ;
Note that the third line needs to force conversion. Use the model:
List
Here, List is declared into a String type list. List is a model interface with a type of parameters. The type parameters in this example are string.
2. Define simple model
See the implementation of List and Iterator interfaces in J2SE5.0:
Public interface list
We are more familiar with the code, but increasing angle brackets. The contents of the spare brackets define the form of the interface List and Iterator. Type parameters can be used in a model declaration, such as classes, and interface statements.
Once the model is declared, you can use it. List
Regardless of List
List
The output is TRUE.
In general, the form type parameters are uppercase, try to use a single letter, and many container classes use E as parameters.
3. Fan and inheritance
Consider the following code, do you think it will be wrong?
String s = "smallnest@163.com"; Object O = S:
Of course, the String class inherits the Object class, which will not be wrong. But what about the following code?
List
Compile out!
Yes, List
4. Wildcard
Consider the following method: public void printcollection (Collection
In fact, the above method is not universal, it can only print collect
In order to solve this problem, you can use wildcard:
Public Void PrintCollection (Collection > c) {for (Object O: c) {system.out.printf ("% s% n", o);}}
COLLECTION > A collection of unknown types. The question mark represents various types.
When the data in the collection is read, we use the Object type. This can be done, because regardless of the type of unknown type, its data inherits the Object class, then consider the following code:
Collection > C = new arraylist
This is wrong, because we don't know? What type of representative is, so we can't add Object to a collection, which does not match the type.
5. Limited wildcard
Consider the following code
Class Man {public string name = "";} Class Goodman Extends Man {public string name = "} class badman extends man {public string name ="}
Consider the following model method:
Public void printname (List
This model method can only display the List
Public void printname (List extends man> men) {for (Man Man: Men) {system.out.println ("Name: man.name);}}
This is used here? Extends Man instead of Man, indicating that subclasses accepted by any Mana are parameters.
Similar to the previous code, the following code is incorrect:
Public void adman (list extends man> men) {goodman good = new goodman (); good.name = "晁 晁 攀"; Men.Add (Good);
The reason is also very simple, because? On behalf of all the classes of the Man, you can't guarantee the Goodman class.
Similar to this method:
Public void adman (list super goodman> men) {goodman good = new goodman (); good.name = "晁 晁 攀"; Men.Add (Good);
6. Feman method
Consider the following code, we add an array of content to a collection PUBLIC VOID COPYARRAYTOCOLLECTION (Man [] MEN, Collection > C) {for (Man MAN: MEN) {C.Add (Man);}}
This code is wrong!
Because we don't know the type of collections C, you cannot add data of the MAN type to a collection.
You can use a model method to solve:
PUBLIC
Here T is a form type parameter.
When should I use a general method? When should I use a wildcard?
Consider the following example:
Interface Collection
Rewriting into a general method
Interface Collection
However, each method T is only used once, the return value does not rely on the formal parameters, and other parameters do not rely on the formal parameters. This shows that the argument is used as a polymorphism, and it should be used in this case.