【Java学习笔记】Java6泛型实例
作者:gnuhpc
出处:http://www.cnblogs.com/gnuhpc/
你若是不使用泛型,则会这样处理数据类型不确定的问题:
class Stash {
private Object x;
void set(Object x) {
this.x = x;
}
Object get() {
return(x);
}
}
public class StashOne {
public static void main(String arg[]) {
Stash stash = new Stash();
stash.set("abcd");
String str = (String)stash.get();
}
}
使用了泛型则:
class StashString {
private String x;
void set(String x) {
this.x = x;
}
String get() {
return(x);
}
}
public class StashTwo {
public static void main(String arg[]) {
StashString stash = new StashString();
stash.set("abcd");
String str = stash.get();
}
}
你也可以在创建对象的时候规定类型:
class Stash<T> {
private T x;
void set(T x) {
this.x = x;
}
T get() {
return(x);
}
}
public class StashThree {
public static void main(String arg[]) {
Stash<String> stash = new Stash<String>();
stash.set("abcd");
String str = stash.get();
}
}
或者在赋值的时候规定:
class Stash<T> {
private T x;
void set(T x) {
this.x = x;
}
T get() {
return(x);
}
}
public class StashFour {
public static void main(String arg[]) {
Stash<Object> stash = new Stash<Object>();
stash.set("abcd");
String str = (String)stash.get();
}
}
要想规定某个参数是某各类及其子类的泛型,则:
class Stash<T extends Number> {
private T x;
void set(T x) {
this.x = x;
}
T get() {
return(x);
}
}
public class StashFive {
public static void main(String arg[]) {
Stash<Integer> istash = new Stash<Integer>();
Integer ten = new Integer(10);
istash.set(ten);
ten = istash.get();
Stash<Double> dstash = new Stash<Double>();
Double pi = new Double(3.14159);
dstash.set(pi);
pi = dstash.get();
}
}
对于接口:
import java.util.EventListener;
import javax.swing.JTable;
import javax.swing.undo.UndoManager;
class Stash<T extends EventListener> {
private T x;
void set(T x) {
this.x = x;
}
T get() {
return(x);
}
}
public class StashSix {
public static void main(String arg[]) {
Stash<JTable> tablestash = new Stash<JTable>();
JTable table = new JTable();
tablestash.set(table);
table = tablestash.get();
Stash<UndoManager> dstash = new Stash<UndoManager>();
UndoManager unman = new UndoManager();
dstash.set(unman);
unman = dstash.get();
}
}
而你要是想既规定类又规定实现了某一个接口,那么:
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.awt.Transparency;
class Stash<T extends Image & Transparency> {
private T x;
void set(T x) {
this.x = x;
}
T get() {
return(x);
}
}
public class StashSeven {
public static void main(String arg[]) {
Stash<BufferedImage> bufstash = new Stash<BufferedImage>();
BufferedImage bufimage = new BufferedImage(50,50,0);
bufstash.set(bufimage);
bufimage = bufstash.get();
}
}
而通配符的泛型类可以使你在创建泛型类的指针时可以模糊处理:
class Stash<T> {
private T x;
void set(T x) {
this.x = x;
}
T get() {
return(x);
}
}
public class StashEight {
public static void main(String arg[]) {
Stash<? extends Number> numberstash;
Stash<Integer> integerstash;
integerstash = new Stash<Integer>();
integerstash.set(new Integer(10));
numberstash = integerstash;
Number number = numberstash.get();
System.out.println(number.toString());
Stash<Double> doublestash;
doublestash = new Stash<Double>();
doublestash.set(new Double(3.14159));
numberstash = doublestash;
Double dnumber = (Double)numberstash.get();
System.out.println(dnumber.toString());
}
}
泛型还可以嵌套:
class Pair<T,U> {
private T left;
private U right;
Pair(T l, U r) {
left = l;
right = r;
}
public T getLeft() {
return(left);
}
public U getRight() {
return(right);
}
}
class Stash<T> {
private T t;
void set(T t) {
this.t = t;
}
T get() {
return(t);
}
}
public class Nesting {
@SuppressWarnings("unchecked")
public static void main(String arg[]) {
Stash<Pair<String,Long>> sp;
sp = new Stash<Pair<String,Long>>();
Pair pair = new Pair("Average",new Long(320));
sp.set(pair);
pair = sp.get();
System.out.println(pair.getLeft() + " " + pair.getRight());
}
}
另外泛型不只是类,方法也可以泛型:
import java.awt.Color;
public class GenericMethod {
public static void main(String arg[]) {
GenericMethod gm = new GenericMethod();
gm.objtype("abcd");
gm.gentype("abcd");
gm.objtype(Color.green);
gm.gentype(Color.green);
}
public void objtype(Object t) {
System.out.println(t.getClass().getName());
}
public <T> void gentype(T t) {
System.out.println(t.getClass().getName());
}
}