你若是不使用泛型,则会这样处理数据类型不确定的问题:
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 {
private T x;
void set(T x) {
this.x = x;
}
T get() {
return(x);
}
}
public class StashThree {
public static void main(String arg[]) {
Stash stash = new Stash();
stash.set("abcd");
String str = stash.get();
}
}
或者在赋值的时候规定:
class Stash {
private T x;
void set(T x) {
this.x = x;
}
T get() {
return(x);
}
}
public class StashFour {
public static void main(String arg[]) {
Stash stash = new Stash();
stash.set("abcd");
String str = (String)stash.get();
}
}
要想规定某个参数是某各类及其子类的泛型,则:
class Stash {
private T x;
void set(T x) {
this.x = x;
}
T get() {
return(x);
}
}
public class StashFive {
public static void main(String arg[]) {
Stash istash = new Stash();
Integer ten = new Integer(10);
istash.set(ten);
ten = istash.get();
Stash dstash = new Stash();
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 {
private T x;
void set(T x) {
this.x = x;
}
T get() {
return(x);
}
}
public class StashSix {
public static void main(String arg[]) {
Stash tablestash = new Stash();
JTable table = new JTable();
tablestash.set(table);
table = tablestash.get();
Stash dstash = new Stash();
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 {
private T x;
void set(T x) {
this.x = x;
}
T get() {
return(x);
}
}
public class StashSeven {
public static void main(String arg[]) {
Stash bufstash = new Stash();
BufferedImage bufimage = new BufferedImage(50,50,0);
bufstash.set(bufimage);
bufimage = bufstash.get();
}
}
而通配符的泛型类可以使你在创建泛型类的指针时可以模糊处理:
class Stash {
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 integerstash;
integerstash = new Stash();
integerstash.set(new Integer(10));
numberstash = integerstash;
Number number = numberstash.get();
System.out.println(number.toString());
Stash doublestash;
doublestash = new Stash();
doublestash.set(new Double(3.14159));
numberstash = doublestash;
Double dnumber = (Double)numberstash.get();
System.out.println(dnumber.toString());
}
}
泛型还可以嵌套:
class Pair {
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 {
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> sp;
sp = new Stash>();
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 void gentype(T t) {
System.out.println(t.getClass().getName());
}
}
分享到: