JDK 1.5 Characteristics Example 1 - Generics

xiaoxiao2021-03-06  54

Special note: Although Sun named the new version of JDK by Chinese habit 5.0, although the new version of the new version is so much, I still have to call new versions as JDK 1.5.

Generics is the most important feature of JDK 1.5, mainly to process Collection.

Detailed generics tutorial: http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf.

The following code is debugged in JDK 1.5.

Code Example 1. Demo.java

Package maoxiang.examples.jdk15.generics;

Import java.util.arraylist;

Import java.util.collection;

Import java.util.hashmap;

Import java.util.linkedList;

Import java.util.list;

Import java.util.map;

/ **

* @Author Mao Xiang

*

* Demonstrate how to use the generics feature. The code comes from the generics tutorial:

* http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf

*

* Generics is similar to Templates in C .

* the difference:

* 1.

* 2.

* /

Public class demo {

Public static void main (String [] args) {

}

/ **

* The simplest usage

* /

Public void test1 () {

// previous usage

// list myintlist = new linkedList (); // 1

//myintlist.add(new integer (0)); // 2

// integer x = (integer) MyintList.ITerator (). Next (); // 3 requires forced conversion

// 1.5 usage

List Myintlist = new linkedList (); // 1 '

Myintlist.add (new integer (0)); // 2 '

Integer x = MyintList.iterator (). Next (); // 3 '

}

/ **

* Usage of anonymous characters

* /

Public void test2 () {

List list = new arraylist ();

// Print a collection by anonymous character

Wildcards (List);

Wildcards1 ();

/ *

* If Wildcards2 is defined as Wildcards2 (List Shapes)

* The following call error

* /

Wildcards2 (List);

}

Public void Wildcards (Collection c) ​​{

// previous usage

// Iterator i = C.Itemrator ();

// for (int K = 0; k

//

Log (i.next ());

//}

//1.5 usage

// Collection C

For (Object E: C) {

Log (e);

}

}

Public void wildcards1 () {

// Collection C = new arraylist ();

//c.add (new object ()); // compile time error

// The above is a wrong usage, because the type of C cannot be determined, the add, but Get can. The correct usage is as follows: arraylist c = new arraylist ();

C.Add ("test");

List List = C;

LOG (C.Get (0));

}

Public Void Wildcards2 (List Shapes) {

// list Shapes Definition can only accept list shapes, or you cannot accept list

For (Shape S: Shapes) {

s.draw ();

}

// The following means is wrong, because the parameters are declared as Extends Shpape, and the Rectangle is not a SHAPE subclass, which belongs to unsecurity calls.

//shapes.add(0, new rectangle ());

Map alldrivers = new hashmap ();

Census.addRegistry (alldrivers);

// The following is allowed, because Drivers are clearly defined.

List Drivers = New ArrayList ();

Census.add (drivers);

}

/ **

* Generic Methods usage

*

* /

Public void test3 () {

/ / Suitable for various types of functions

Object [] OA = New Object [100];

Collection CO = New ArrayList ();

FromArrayTocollection (OA, CO); // Tinferred to Be Object

String [] sa = new string [100];

Collection CS = New ArrayList ();

FromArrayTocollection (SA, CS); // Tinferred to Be String

FromArrayTocollection (SA, CO); // Tinferred to Be Object

Integer [] IA = new integer [100];

Float [] fa = new float [100];

Number [] na = new number [100];

Collection CN = New ArrayList ();

FromArrayTocollection (IA, CN); // Tinferred to Be Number

FromArrayTocollection (Fa, CN); // Tinferred to Be Number

FromArrayTocollection (NA, CN); // Tinferred to Be Number

FromArrayTocollection (NA, CO); // Tinferred to Be Object

//test.fromaaraytocollection (na, cs); // error usage

}

Public Void fromArraytocollection (T [] A, Collection c) {

For (t o: a) {

/ / If the parameter is defined as a collection C below to write error C.Add (o); // Compile Time Error

}

}

/ **

* generics nested usage

* @Param Shapes

* /

Public void Drawall (List Shapes) {

List > History = new arraylist > ();

History.add (shapes);

For (Shape S: Shapes) {

s.draw ();

}

}

/ **

*

*

* /

Public void test4 () {

List L1 = New ArrayList ();

List l2 = new arraylist ();

System.out.print (l1.getclass () == l2.getClass ());

// Print to True,

}

/ **

* Error usage

* /

Public void test5 () {

Collection CS = New ArrayList ();

/ / The following is the wrong use

// IF (cs instanceof color ) {} // illegal

// The following is a warning usage

// Collection CSTR = (Collection ) cs; // unchecked

// Warning

}

Public void test6 () {

// Error usage

// list [] lsa = new list [10]; // not real allowed

List [] Lsa = new list [10]; // ok, array of unbounded wildcard

// Type

Object o = lsa;

Object [] OA = (Object []) O;

List li = new arraylist ();

Li.Add (New Integer (3));

OA [1] = li; // Correct

// String S = LSA [1] .get (0); // Run-Time Error - ClassCastException

// string s = lsa [1] .get (0); // Run Time Error, But We worth Warned

String s = (string) LSA [1] .get (0); // Run Time Error, But Cast IS

// Explicit

}

Public void test7 () {

SINK s = null;

SINK S1 = NULL;

Collection CS = NULL;

String str = WriteAll (CS, S1);

// String str = WriteAll (CS, S); // invalid call

Object obj = writeall1 (cs, s); / / correct call

Str = WriteAll2 (CS, S1); // Correct call}

Public T Writeall (Collection COLL, SINK snk) {

T last = null;

For (t t: coll) {

Last = T;

SNK.FLUSH (LAST);

}

Return Last;

}

PUBLIC T WRITEALL1 (Collection Coll, Sink snk) {

T last = null;

For (t t: coll) {

Last = T;

SNK.FLUSH (LAST);

}

Return Last;

}

Public T WriteAll2 (Collection COLL, SINK snk) {

T last = null;

For (t t: coll) {

Last = T;

SNK.FLUSH (LAST);

}

Return Last;

}

// Print

Private void log (Object ob) {

System.out.print (OB);

}

}

// Auxiliary definition

Abstract class shape {

Public Abstract void Draw ();

}

Class Circle Extends Shape {

Private Int x, y, radius;

Public void draw () {

}

}

Class Rectangle Extends Shape {

Private int x, y, width, height;

Public void draw () {

}

}

Class Person {

}

Class Driver Extends Person {

}

Class Census {

Public Static Void AddRegistry (Map Registry) {

}

Public Static Void Add (List Persons) {

}

}

Class collections {

Public Static Void Copy (List DEST, LIST SRC) {

}

}

Code Example 2. Sink.java

Package maoxiang.examples.jdk15.generics;

/ **

*

* @Author Mao Xiang

*

* Define an interface template to simplify the definition of the interface

*

* /

Interface Sink {

Public void flush (e t);

}

/ *

* If it is the previous definition, it is clear that the interface to be various types is clear.

* Interface Sink {

*

* Public Void Flush (String STR);

* Public void flush (Object obj);

* Public Void Flush (Integer Test);

* ...

*}

* /

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

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