Java 继承泛型类和实现泛型接口

时间:2022-06-18
本文章向大家介绍Java 继承泛型类和实现泛型接口,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

泛型也可以继承和实现接口

public class Test{

	public static void main(String[] args) {
	  
	  
	}
}
class Father<T>{
	
}
interface ARB<E>{
	
}
class child<T,E> extends Father<T> implements ARB<E>{
	
}

泛型继承的四种情况

  • 全部继承

子类泛型可以比父类多

public class Test{

	public static void main(String[] args) {
	  Father<Integer,String> father = new child<>(1, "2");
	  child<String,String ,Boolean> child = new child<>("1","2");
	  
	}
}
class Father<T1, T2>{
	T1 t1;
	T2 t2;
	
	public Father(T1 t1,T2 t2){
		this.t1 = t1;
		this.t2 = t2;
		System.out.println(this.t1.getClass());
		System.out.println(this.t2.getClass());
	}
	
	
}
class child<T1, T2, T3> extends Father<T1, T2> {

	public child(T1 t1, T2 t2) {
		super(t1, t2);
	}
	
}
  • 部分继承

同样子类泛型也可以比父类多

public class Test{

	public static void main(String[] args) {
	  Father<Integer,String> father = new child<>(1, "2");
	  child<String,String ,Boolean> child = new child<>("1","2");
	  
	}
}
class Father<T1, T2>{
	T1 t1;
	T2 t2;
	
	public Father(T1 t1,T2 t2){
		this.t1 = t1;
		this.t2 = t2;
		System.out.println(this.t1.getClass());
		System.out.println(this.t2.getClass());
	}
	
	
}
class child<T1, T2, T3> extends Father<T1,String> {//继承时将父类一个泛型实例化

	public child(T1 t1, String t2) {
		super(t1, t2);
	}
	
}

实现父类泛型

子类将父类全部实现,子类独有,不再是继承的

public class Test{

	public static void main(String[] args) {
	  child<String,String ,String> child = new child<>(1,"2");
         //无论怎么写第一个都是整形,第二个是字符串
	  
	}
}
class Father<T1, T2>{
	T1 t1;
	T2 t2;
	
	public Father(T1 t1,T2 t2){
		this.t1 = t1;
		this.t2 = t2;
		System.out.println(this.t1.getClass());
		System.out.println(this.t2.getClass());
	}
	
	
}
class child<T1, T2, T3> extends Father<Integer,String> {

	public child(Integer t1, String t2) {
		super(t1, t2);
	}
	
}

不实现父类泛型 

父类所有成员默认为Object类型

public class Test{

	public static void main(String[] args) {
	  child<String,String ,String> child = new child<>("1","2");
	  
	}
}
class Father<T1, T2>{
	T1 t1;
	T2 t2;
	
	public Father(T1 t1,T2 t2){
		this.t1 = t1;
		this.t2 = t2;
		System.out.println(this.t1.getClass());
		System.out.println(this.t2.getClass());
	}
	
	
}
class child<T1, T2, T3> extends Father{

	public child(Object t1, Object t2) {
		super(t1, t2);
		// TODO Auto-generated constructor stub
	}


	
}