package learn;
import java.util.*;
public class learn1 {
/**
* @param args
* 泛型实例
*/
public static void main(String[] args) {
Point<Integer,Integer> pt1= new Point<Integer,Integer>();
pt1.setX(15);
pt1.setY(25);
pt1.print();
pt1.fprint(15, 25);
Point<String,Integer> pt2 = new Point<String,Integer>();
pt2.setX("zfeig");
pt2.setY(68);
pt2.print();
pt2.fprint("zfeig",68);
Point<Double,String> pt3 = new Point<Double,String>();
pt3.setX(15.04);
pt3.setY("hello");
pt3.print();
pt3.fprint(15.04, "hello");
}
}
class Point<T1,T2>{
T1 x;
T2 y;
public void setX(T1 x){
this.x=x;
}
public void setY(T2 y){
this.y=y;
}
public T1 getX(){
return x;
}
public T2 getY(){
return y;
}
public void print(){
System.out.println("普通方法输出结果为:x="+getX()+",y="+getY());
}
public <T1,T2> void fprint(T1 x,T2 y){
T1 m=x;
T2 n=y;
System.out.println("泛型方法,point输出的值为:("+m+","+n+")");
}
}
java学习之泛型例子
最新推荐文章于 2024-02-16 20:47:06 发布