《java开发实战经典》李兴华——C10. 泛型

一、为什么使用泛型

类转换异常,出现了类型安全问题

二、泛型应用

1.泛型的基本应用

在定义类时,通过一个标识符表示类的种某属性的类型,或某种方法的返回值及参数类型。

这样在实例化时只需要指定好需要的类型即可。

2.示例:

public class Test{
	public static void main(String[] args) {
		Point p = new Point();
		p.setVar("李兴华");
		System.out.println(p.getVar().length());
	}
}

class Point {
	private T var;
	public T getVar() {
		return var;
	}
	public void setVar(T var) {
		this.var = var;
	}
}

3.泛型中的构造方法

class Point {
	private T x;
	private T y;
	
	public Point(T x,T y) {
		this.setX(x);
		this.setY(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 Test{
	public static void main(String[] args) {
		Point p = new Point(11, 22);
		int x = p.getX();
		int y = p.getY();
		System.out.println("整数表示,X坐标:"+x);
		System.out.println("整数表示,Y坐标:"+y);
	}
}

4.指定多个泛型类型

class Notepad {
	private K key;
	private V value;
	public K getKey() {
		return key;
	}
	public void setKey(K key) {
		this.key = key;
	}
	public V getValue() {
		return value;
	}
	public void setValue(V value) {
		this.value = value;
	}
}

public class Test{
	public static void main(String[] args) {
		Notepad n = new Notepad();
		n.setKey("李兴华");
		n.setValue(30);
		System.out.println("姓名:"+n.getKey());
		System.out.println("年龄:"+n.getValue());
	}
}

三、泛型的安全警告

用户在声明对象时最好指定其内部的数据类型,否则就会出现不安全的警告信息。

《java开发实战经典》李兴华——C10. 泛型_第1张图片

编译时警告,但不影响程序运行。

四、通配符

1.匹配任意类型的通配符

class Info {
	private T var;

	public T getVar() {
		return var;
	}

	public void setVar(T var) {
		this.var = var;
	}

	@Override
	public String toString() {
		return this.var.toString();
	}
}

public class Test{
	public static void main(String[] args) {
		Info i = new Info();
		Info j = new Info();
		i.setVar("李兴华");
		j.setVar(30);
		fun(i);
		fun(j);
		System.out.println("内容:"+i.getVar());
	}
	public static void fun(Info temp) {//?:通配符
		System.out.println("内容:"+temp);
	}
}

2.受限泛型

1)设置上限: ? extends  类

                         泛型标识   extends  类

2)设置下限:?  super  类

                        泛型标识   super   类

class Info {
	private T var;
	public T getVar() {
		return var;
	}
	public void setVar(T var) {
		this.var = var;
	}
        public String toString() {
		return this.getVar().toString();
	}

}

public class Test{
	public static void main(String[] args) {
		Info i = new Info();
		Info j = new Info();
		i.setVar(6.6f);
		j.setVar(30);
		fun(i);
		fun(j);
	}
	//上限为number,可以传Byte,Short,Long,Integer,Float,Double
	public static void fun(Info temp) {
		System.out.println("内容:"+temp);
	}
}
class Info {
	private T var;
	public T getVar() {
		return var;
	}
	public void setVar(T var) {
		this.var = var;
	}
	public String toString() {
		return this.getVar().toString();
	}
}

public class Test{
	public static void main(String[] args) {
		Info i = new Info();
		Info j = new Info();
		i.setVar(new Object());
		j.setVar("李兴华");
		fun(i);
		fun(j);
	}
	//下限为String,可以传String,Object
	public static void fun(Info temp) {
		System.out.println("内容:"+temp);
	}
}

五、泛型与子类继承的限制

子类的泛型类型不能用父类的泛型类型接收。如Info不能用Info接收。

六、泛型接口

1.定义泛型接口

   interface 接口名称<泛型标识>{ } 

2.泛型接口的两种实现方式

//第一种:在子类得定义上声明泛型类型
interface Info{
	public T getVar();
}

class InfoImpl implements Info{

	private T var;
	
	public InfoImpl(T var){
		this.setVar(var);
	}
	
	public void setVar(T var) {
		this.var = var;
	}
	
	@Override
	public T getVar() {
		return this.var;
	}
}

public class Test{
	public static void main(String[] args) {
		Info i = null;
		i = new InfoImpl("lll");
		System.out.println(i.getVar());
	}
}
//第二种:直接在接口中指定具体类型
interface Info{
	public T getVar();
}

class InfoImpl implements Info{

	private String var;
	
	public InfoImpl(String var){
		this.setVar(var);
	}
	
	public void setVar(String var) {
		this.var = var;
	}
	
	@Override
	public String getVar() {
		return this.var;
	}
}

public class Test{
	public static void main(String[] args) {
		Info i = null;
		i = new InfoImpl("lll");
		System.out.println(i.getVar());
	}
}

七、泛型方法

 

你可能感兴趣的:(《java开发实战经典》李兴华——C10. 泛型)