มาทำความรู้จักกับ Generic ใน Java Tiger
posted on 29 Oct 2004 19:09 by somkiatการใช้งาน Generic ใน Java 5.0
ใน J2SE 1.4.X นั้นมี Colection ที่จะเก็บ object ต่างๆ ไว้ แต่ว่า Collection ไม่สามารถเก็บชนิดข้อมูลที่เป็น primitive type ได้
แต่ว่าสามารถเก็บ Wrapper Class ของ primitive type ได้ เมื่อต้องการเก็บข้อมูล primitive ก็แปลงมาเป็น Wrapper Object ได้
เช่น
int data = 1;
Integer obje1 = new Integer(data);
ใน Collection object นั้นเมื่อทำการดึงข้อมูลออกมาจะมี type เป็น object เพื่อต้องการให้ type safty ดังนั้นเราจำเป้นจะต้อง
cast type ของ object ให้เป็น Object type ที่ต้องการ ดังนั้นจะเห็นได้ว่าการจัดการ collection object นั้นเป็นเรื่องที่ยากและยัง
เสี่ยงต่อการเกิด error ในขณะ runtime (Runtime Error) อีกด้วย
ดังนั้นใน java 5.0 หรือ codename คือ "Tiger" นั้นได้เพิ่มความสามารถของภาษามาให่ซึ่งก็คือ "Generic" ซึ่งจะทำให้การ
จัดการ collection ง่ายขึ้น เช่น ไม่จำเป็นต้อง cast type ดังนั้นปัญหาของ Runtime Error จึงไม่มีทางเกิดขึ้นอย่างแน่นอน
เพราะว่าจะทำการตรวจสอบ error ในระหว่างการ compile program นั่นเอง
ตัวอย่างการใช้งาน Generric ในการจัดการ Collection
//ใน Version 1.4.x
LinkedList list = new LinkedList();
list.add(new Integer(1));
Integer value = (Integer) list.get(0);
//ใน version 5.0
LinkdeList
List.add(new Integer(1));
Integer value = list.get(0);
และยังสามารถใช้คุณสมบัติ autoboxing ได้อีกด้วย ดังนี้
LinkdeList
List.add(1);
Integer value = list.get(0);
Implement Generic type
เราสามารถสร้าง generic type มาใช้เองได้ เช่น
interface List
void add(E x);
Iterator
}
interface Iterator
E next();
boolean hasNext();
}
class LinkedList
// implementation
}
ตัวอย่างการเรียกใช้งาน
LinkedList
สามารถกำหนดเป็น promitive type ได้
Generic Method
generic ไม่ใช่จำกัดแค่เพียง class, interface เท่านั้น แต่ว่ายังสามารถใช้ใน method ได้อีกด้วยครับ โดยสามาถใช้ใน
- non-static method
- static method
- constructor
แต่ว่า syntax อาจจะแตกต่างไปบ้างเล็กน้อย และการเรียกใช้ method ก็เหมือนกับ non-generic method ทั่วไป
ตัวอย่าง
void printCollection(Collection> c) {
for(Object o:c) {
System.out.println(e);
}
}
> คือ unknow type หรือเรียกว่า widecards
Widecards มี 3 ชนิดคือ
1. "? extends Type"" คือการ extends นั่นเอง subsclass ของ Type นั่นเอง
2. "? super Type" คือ Super class ของ Type
3. "?" คือ any type หรือว่า type อะไรก็ได้
ตัวอย่าง
public void draw(List extends Shape> shape) {
// rest of the code is the same
}
และ
public static
Object a[] = list.toArray();
Arrays.sort(a);
ListIterator
for(int j=0; j
i.set((t)a[j]);
}
}
Java Generics vs. C++ Templates
While generics look like the C++ templates, it is important to note that they are not the same. Generics simply provide
compile-time type safety and eliminate the need for casts. The main difference is encapsulation: errors are flagged where they
occur and not later at some use site, and source code is not exposed to clients. Generics use a technique known as type erasure
as described above, and the compiler keeps track of the generics internally, and all instances use the same class file at
compile/run time.
A C++ template on the other hand is just a fancy macro processor; whenever a template class is instantiated with a new class,
the entire code for the class is reproduced and recompiled for the new class.
Conclusion
Generics are a new core feature in J2SE 5.0, and a major addition to the core language. This feature provides a useful abstract
and compile-time type safety for collections and eliminates the drudgery of casting. This article provided an overview and
introduction to Java generics, and showed how to use generics as well as write your own. The examples provided in this article
demonstrate how useful this new core feature is.
แก้ไขเมื่อ 29/10/2547 19:09:45

#1 By (203.149.59.130 /unknown) on 2006-10-11 13:27