java-泛型的继承

目录

父类或接口中的泛型可以被子类继承

泛型继承的四种情况

1.全部继承

2.部分继承 

3.实现父类泛型 

4.不实现父类泛型


父类或接口中的泛型可以被子类继承

继承泛型类和泛型接口

class ExtendClass{

}

----->

class SubClass extends ExtendClass{

}


interface TestInterface{

}

----->

class SubClass2 implements  TestInterface{

}

public class Demo {
	public static void main(String[] args) {
		
	}
}

class Father{
	T t;
	public Father(T t) {
		this.t=t;
	}
}
interface TestInterface{
	
}
class Child extends Father implements TestInterface{

	public Child(T t) {
		super(t);
	}
}

泛型继承的四种情况

父类--> abstract class Father{}  

1.全部继承

class Child extends Father{} 

public class Demo {
	public static void main(String[] args) {
		Father f=new Child<>(1,"2");
		//Child c=new Child<>(1,"2");   此行会报错
		Child c=new Child<>(1,true);	//正确,不会报错(T1,T3继承)
	}
}

class Father{
	T1 t1;
	T2 t2;
	public Father(T1 t1, T2 t2) {
		
		this.t1 = t1;
		this.t2 = t2;
		System.out.println("t1的类型:"+this.t1.getClass());
		System.out.println("t2的类型:"+this.t2.getClass());
	}
	
}
 /*class Childextends Father{

	public Child(T1 t1, T2 t2) {
		super(t1, t2);
		
	}
	
}*/
//T3继承

class Childextends Father{

	public Child(T1 t1, T3 t2) {
		super(t1, t2);
		
	}
	
}

//运行结果
t1的类型:class java.lang.Integer
t2的类型:class java.lang.String
t1的类型:class java.lang.Integer
t2的类型:class java.lang.Boolean

2,部分继承 

calss Child extends Father{}

public class Demo {
	public static void main(String[] args) {
	
		Child c=new Child<>("1","2");	//第二三个泛型可以是任意类型,不影响
}
}
class Father{
	T1 t1;
	T2 t2;
	public Father(T1 t1, T2 t2) {
		
		this.t1 = t1;
		this.t2 = t2;
		System.out.println("t1的类型:"+this.t1.getClass());
		System.out.println("t2的类型:"+this.t2.getClass());
	}
	
}

//T3继承

class Childextends Father{

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


//运行结果
t1的类型:class java.lang.String
t2的类型:class java.lang.String

3.实现父类泛型 

class Child extends Father{}

子类在继承父类的时候,将父类所有的泛型都实现了,这样子类就不会继承父类的泛型了,即使创建了泛型,也是子类独有的。

public class Demo {
	public static void main(String[] args) {

	//前面类型改变不影响结果
		Child c=new Child<>(1,"2");	
	}
}

class Father{
	T1 t1;
	T2 t2;
	public Father(T1 t1, T2 t2) {
		
		this.t1 = t1;
		this.t2 = t2;
		System.out.println("t1的类型:"+this.t1.getClass());
		System.out.println("t2的类型:"+this.t2.getClass());
	}
	
}


class Childextends Father{//把所有泛型实现,子类再多的泛型与父类无关

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


//运行结果
t1的类型:class java.lang.Integer
t2的类型:class java.lang.String

4.不实现父类泛型

class Child extends Father{}

忽略父类泛型,既不实现,也不继承;这样不会报错,但是父类所有泛型成员会默认为Object类型

public class Demo {
	public static void main(String[] args) {

	//前面类型改变不影响结果
		Child c=new Child<>("1","2");	//写什么都可以
		System.out.println("--");
		Child d=new Child<>(1,"2");	
	}
}

class Father{
	T1 t1;
	T2 t2;
	public Father(T1 t1, T2 t2) {
		
		this.t1 = t1;
		this.t2 = t2;
		System.out.println("t1的类型:"+this.t1.getClass());
		System.out.println("t2的类型:"+this.t2.getClass());
	}
	
}


class Childextends Father{

	public Child(Object t1, Object t2) {
		super(t1, t2);
		// TODO 自动生成的构造函数存根
	}
}


//运行结果
t1的类型:class java.lang.String
t2的类型:class java.lang.String
--
t1的类型:class java.lang.Integer
t2的类型:class java.lang.String

 

 

 

 

 

 

你可能感兴趣的:(学习笔记,Java,java,开发语言)