gpt4 book ai didi

java - 泛型和子类型

转载 作者:行者123 更新时间:2023-11-29 07:10:34 25 4
gpt4 key购买 nike

Create a class with a generic type T. A subclass of the class that defines a second type parameter, called V. Call the methods from both classes using the object. Create 2 objects with different objects

这就是问题。

如果有人认为这不是家庭作业。我正在做一些 java 论文来练习。我不明白如何“用不同的对象创建 2 个对象”。谁能帮忙?

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());
}
}

最佳答案

问题的最后一部分:

Create 2 objects with different objects

Gen2<Integer,String> inst = new Gen2<Integer,String>(1, "robin");  

关于java - 泛型和子类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14261354/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com