|

楼主 |
发表于 2018-9-17 22:56:36
|
显示全部楼层
//泛型学习
class Point <Tx,Ty>
{
private Tx x;
private Ty y;
public void setX(Tx x)
{
this.x=x;
}
public void setY(Ty y)
{
this.y=y;
}
public Tx getX()
{
return this.x;
}
public Ty getY()
{
return this.y;
}
}
//*****************************************
public class GenericsDemo01
{
public static void main(String[] args)
{
Point<Integer,String> p=new Point<Integer,String>();
p.setX(10);
p.setY("用string型表示Y坐标");
//int x=(Integer)(p.getX());
//int y=(Integer)(p.getY());
System.out.println("整数表示,X的坐标为:"+p.getX());
System.out.println("字符串表示,y的坐标为:"+p.getY());
Point<Float,Boolean> p2=new Point<Float,Boolean>();
p2.setX(3.1415F);
p2.setY(true);
System.out.println("浮点数表示,X的坐标为:"+p2.getX());
System.out.println("布尔型表示,y的坐标为:"+p2.getY());
}
} |
|