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
}
}
- java教程
- Java快速入门
- Java 开发环境配置
- Java基本语法
- Java 对象和类
- Java 基本数据类型
- Java 变量类型
- Java 修饰符
- Java 运算符
- Java 循环结构
- Java 分支结构
- Java Number类
- Java Character类
- Java String类
- Java StringBuffer和StringBuilder类
- Java 数组
- Java 日期时间
- Java 正则表达式
- Java 方法
- Java 流(Stream)、文件(File)和IO
- Java 异常处理
- Java 继承
- Java 重写(Override)与重载(Overload)
- Java 多态
- Java 抽象类
- Java 封装
- Java 接口
- Java 包(package)
- Java 数据结构
- Java 集合框架
- Java 泛型
- Java 序列化
- Java 网络编程
- Java 发送邮件
- Java 多线程编程
- Java Applet基础
- Java 文档注释