一,Demo1:
public class Point<T> {
public T x;
public T y;
public T getX() {
return x;
}
public void setX(T x) {
this.x = x;
}
public T getY() {
return y;
}
public void setY(T y) {
this.y = y;
}
}
public class GenDemo {
public static void main(String args[])
{
Point<Float> p=new Point<Float>();
p.setX(11.9f);
p.setY(20.8f);
System.out.println(p.getX());
System.out.println(p.getY());
}
}
二,Demo2泛型擦除
public class Point<T> {
public T x;
public T y;
public T getX() {
return x;
}
public void setX(T x) {
this.x = x;
}
public T getY() {
return y;
}
public void setY(T y) {
this.y = y;
}
//构造方法使用泛型
Point(T x,T y)
{
this.setX(x);
this.setY(y);
}
}
public class GenDemo {
public static void main(String args[])
{
Point<Float> p=new Point<Float>(11.9f,20.8f);
System.out.println(p.getX());
System.out.println(p.getY());
//泛型擦除后按照object类型操作
Point p1=new Point(11.2f,11.3f);
System.out.println(p1.getX());
System.out.println(p1.getY());
}
}
三,泛型通配符
泛型里面不能够自动转型
public class GenDemo {
public static void main(String args[])
{
Point<Float> p=new Point<Float>(11.9f,20.8f);
System.out.println(p.getX());
System.out.println(p.getY());
//泛型擦除后按照object类型操作
Point p1=new Point(11.2f,11.3f);
System.out.println(p1.getX());
System.out.println(p1.getY());
//泛型通配符接收
Point<Object> p3=new Point<Object>();
Point<Integer> p4=new Point<Integer>();
fun(p3);
fun(p4);
}
//泛型通配符接收不同的限定类型
public static void fun(Point<?> po)
{
System.out.println(po.getX());
System.out.println(po.getY());
}
}
四,泛型上限
//设置泛型上限,那么T的数据类型不能超过Number
public class Point<T extends Number> {
public T x;
public T y;
public T getX() {
return x;
}
public void setX(T x) {
this.x = x;
}
public T getY() {
return y;
}
public void setY(T y) {
this.y = y;
}
Point(){}
//构造方法使用泛型
Point(T x,T y)
{
this.setX(x);
this.setY(y);
}
}
public class GenDemo {
public static void main(String args[])
{
Point<Float> p=new Point<Float>(11.9f,20.8f);
System.out.println(p.getX());
System.out.println(p.getY());
//泛型擦除后按照object类型操作
Point p1=new Point(11.2f,11.3f);
System.out.println(p1.getX());
System.out.println(p1.getY());
//泛型通配符接收
Point<Object> p3=new Point<Object>(1,2);
Point<Integer> p4=new Point<Integer>(6,9);
fun(p3);
fun(p4);
//最大操作父类为Number
Point<Integer> p6=new Point<Integer>();
}
//泛型通配符接收不同的限定类型
public static void fun(Point<? extends Number> po)
{
System.out.println(po.getX());
System.out.println(po.getY());
}
}
五,泛型下限
//设置泛型上限,那么T的数据类型不能超过Number
public class Point<T> {
public T x;
public T y;
public T getX() {
return x;
}
public void setX(T x) {
this.x = x;
}
public T getY() {
return y;
}
public void setY(T y) {
this.y = y;
}
Point(){}
//构造方法使用泛型
Point(T x,T y)
{
this.setX(x);
this.setY(y);
}
}
public class GenDemo {
public static void main(String args[])
{
Point<Float> p=new Point<Float>(11.9f,20.8f);
System.out.println(p.getX());
System.out.println(p.getY());
//泛型擦除后按照object类型操作
Point p1=new Point(11.2f,11.3f);
System.out.println(p1.getX());
System.out.println(p1.getY());
//泛型通配符接收
Point<Object> p3=new Point<Object>(1,2);
Point<Integer> p4=new Point<Integer>(6,9);
fun(p3);
//最大操作父类为Number
Point<Integer> p6=new Point<Integer>();
}
//泛型通配符接收不同的限定类型
//在泛型方法中设定泛型的下限
public static void fun(Point<? super String> po)
{
System.out.println(po.getX());
System.out.println(po.getY());
}
}