Java_泛型的继承

Java的泛型可以进行派生:


例子一:

泛型派生为泛型

class Apple<T> {
	private T info;

	public Apple() {

	}

	public Apple(T info) {
		this.info = info;
	}

	public void setInfo(T info) {
		this.info = info;
	}

	public T getInfo() {
		return this.info;
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Apple<String> a1 = new Apple<>("苹果");
		System.out.println(a1.getInfo());
		Apple<Double> a2 = new Apple<>(5.67);
		System.out.println(a1.getInfo());
	}
}

public class A1<T> extends Apple<T> {

	public A1(T info) {
		// TODO Auto-generated constructor stub
		super(info);
	}

	public T getInfo() {
		return super.getInfo();
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		A1 kA1 = new A1<String>("xxxx");
		System.out.println(kA1.getInfo());
	}

}


运行截图:

Java_泛型的继承_第1张图片

你可能感兴趣的:(Java_泛型的继承)