Java中如何实现泛型类和泛型类之间的继承? 80
①A是泛型抽象类,A1是A的子类且A也是泛型类;②A1的泛型参数数据类型是A的泛型参数数据类型的子类。该如何实现A1类的定义?急!!速回!!...
①A是泛型抽象类,A1是A的子类且A也是泛型类;②A1的泛型参数数据类型是A的泛型参数数据类型的子类。该如何实现A1类的定义?急!!速回!!
展开
1个回答
展开全部
A1的泛型参数要有两个,否则无法确定抽象父类的泛型。
public class inher
{
public static void main(String[] args)
{
var a1 = new A1<Child, Parent>();
a1.a = new Parent();
a1.b = new Child();
a1.a.parent = 1;
a1.b.child = 2;
System.out.println(a1.a.parent);
System.out.println(a1.b.child);
}
}
abstract class A<T>
{
public T a;
}
class A1<U extends T, T> extends A<T>
{
public U b;
}
class Parent
{
public int parent;
}
class Child extends Parent
{
public int child;
}
public class inher
{
public static void main(String[] args)
{
var a1 = new A1<Child, Parent>();
a1.a = new Parent();
a1.b = new Child();
a1.a.parent = 1;
a1.b.child = 2;
System.out.println(a1.a.parent);
System.out.println(a1.b.child);
}
}
abstract class A<T>
{
public T a;
}
class A1<U extends T, T> extends A<T>
{
public U b;
}
class Parent
{
public int parent;
}
class Child extends Parent
{
public int child;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询