[英]Generics and subtyping
Create a class with a generic type
T
. 创建一个具有通用类型T
。 A subclass of the class that defines a second type parameter, calledV
. 该类的子类定义了第二个类型参数,称为V
Call the methods from both classes using the object. 使用对象从两个类中调用方法。 Create 2 objects with different objects 用不同的对象创建2个对象
This is the question. 这是问题。
This is not homework if anyone might think. 如果有人想,这不是功课。 I am doing some java papers for practice. 我正在练习一些Java论文。 I don't understand how to "create 2 objects with different objects". 我不明白如何“用不同的对象创建2个对象”。 Can anyone help? 有人可以帮忙吗?
class Gen<T>
{
T obj;
Gen(T ob)
{
obj = ob;
}
T getobj()
{
return obj;
}
}
class Gen2<T, V> extends Gen<T>
{
V obj1;
Gen2(T ob,V ob1)
{
super(ob);
obj1 = ob1;
}
V getobj1()
{
return obj1;
}
}
public class Ch2Lu4Ex3
{
public static void main(String args[])
{
Gen2<String,String> g = new Gen2<String,String>("robin","raj");
System.out.println(g.getobj1());
System.out.println(g.getobj());
}
}
The last part of the question: 问题的最后一部分:
Create 2 objects with different objects 用不同的对象创建2个对象
Gen2<Integer,String> inst = new Gen2<Integer,String>(1, "robin");
Well, you could do things like: 好吧,您可以执行以下操作:
Gen<Integer> gen1 = new Gen<Integer>(10);
Gen2<Integer> gen2 = new Gen2<Integer,String>(10,"Hello");
Or you could combine them since they are on the same hierarchy. 或者,您可以将它们组合在一起,因为它们在同一层次结构中。
Gen<Integer> gen1 = new Gen2<Integer,String>(10,"Hello");
Are you asking for something like: 您是否要求类似:
Gen2<String,String> g = new Gen2<String,String>("robin","raj");
Gen2<Integer,Integer> h = new Gen2<Integer,Integer>(1, 2);
Or am I misunderstanding your question? 还是我误会了你的问题?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.