J2SE5.0 norm programming

xiaoxiao2021-03-06  51

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 list = new linkedList (); list.add ("MAS)); List.Add (" Princeton); List.Add ("Berkeley); string name = list.iterator. NEXT ();

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 {void add (e x); item item ();} public interface iterator {e next (); boolean hasnext ();}

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 is used in the example above. Here is the use of string is the argument, instead of the meticulum E. If List is used, use the argument with the argument EGER.

Regardless of List or list , their classes have only one. Consider the following code:

List list1 = new limitedList (); list list2 = new linkedList (); system.out.println (list1.getclass () == List2.getClass ());

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 s = new linkedlist (); list o = s;

Compile out!

Yes, List and List have no inheritance relationship.

4. Wildcard

Consider the following method: public void printcollection (Collection c) {for (Object O: c) {system.out.printf ("% s% n", o);}}

In fact, the above method is not universal, it can only print collect type, like other Collection , Collection does not be printed because the object type is inconsistent.

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 (); c.Add (new object ()); // !!!!

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 men) {system.out.println ("Name: man.name);}}

This model method can only display the List type data, the following code allows the Man and its subclass.

Public void printname (List 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 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 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 Void CopyArrayTocollection (T [] MEN, Collection c) {for (t man: men) {C.Add (man);}}

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 {public Boolean ContainSall (Collection c); Public Boolean Addall (Collection C);

Rewriting into a general method

Interface Collection {public Boolean ContainSall (Collection C); Public Boolean Addall (Collection c);}

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.

转载请注明原文地址:https://www.9cbs.com/read-83307.html

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