Java泛型类,使用外部类的参数的内部类 [英] Java generic class, inner class using parameter of outer class
问题描述
我有外部泛型类,它有一些内部类,为此我想使用泛型类型的外部类。
然而,我没有了解如何正确使用泛型参数。
示例:
class外部< E扩展CharSequence> {
类Inner扩展ArrayList< E> {
}
void func(){
ArrayList< CharSequence> al = new ArrayList< CharSequence>();
al.add(abc); // OK
CharSequence a = al.get(0); // OK
Inner in = new Inner();
in.add(abc); //错误:方法add(E)在类型ArrayList< E>不适用于参数(字符串)
CharSequence b = in.get(0); // OK
如何声明要使用的内部类相同的泛型类外部类?
谢谢
编辑+解决方案:
我想,下面是示例结果:
抽象类MyGenericClass< E扩展了CharSequence> {
类TheList扩展了ArrayList< E> ; {};
TheList list = new TheList();
}
final class ClassInstance extends MyGenericClass< String> {
};
public class Main {
public static void main(String [] args){
ClassInstance c = new ClassInstance();
c.list.add(abc);
String s = c.list.get(0);
我的要求是有泛型类,通用容器,它将使用与其父类相同的类型参数。
注意:CharSequence / String是使用的示例参数,我的实际用法不同。
解决方案
如何声明内部类使用相同的泛型类型类?
内部已经使用与外
。问题是 func
没有使用它,它使用 CharSequence
。
由于SLak 解释 func
在所有情况下都会做出时髦的东西,除非它在外部< CharSequence>
上被调用:例如当调用 Outer< String>
时,它会将一个 CharSequence
添加到 ArrayList< String>
。
$ b 现在,如果 func
已经有一个具体类型的对象: void func(E e)
{
Inner in = new Inner();
in.add(e);
}
它会做正确的事情。所以编译器允许这样做。
看起来你试图从你面对的某个特定问题创建一个通用问题。如果你描述了你正在尝试做的和失败的事情,你会有更好的机会在这里解决它。
I have outer generic class which has some inner class, for which I'd like to use the generic type of outer class.
However, I don't understand how to use generic parameters correctly.
Example:
class Outer<E extends CharSequence>{
class Inner extends ArrayList<E>{
}
void func() {
ArrayList<CharSequence> al = new ArrayList<CharSequence>();
al.add("abc"); // OK
CharSequence a = al.get(0); // OK
Inner in = new Inner();
in.add("abc"); // Error: The method add(E) in the type ArrayList<E> is not applicable for the arguments (String)
CharSequence b = in.get(0); // OK
}
}
How can I declare inner class to use same generic type of outer class?
Thanks
EDIT + Solution:
Finally I achieved what I wanted, here's example result:
abstract class MyGenericClass<E extends CharSequence>{
class TheList extends ArrayList<E>{};
TheList list = new TheList();
}
final class ClassInstance extends MyGenericClass<String>{
};
public class Main{
public static void main(String[] args){
ClassInstance c = new ClassInstance();
c.list.add("abc");
String s = c.list.get(0);
}
}
My requirement was to have generic class, and also have generic container in it, which would use same type parameter as its parent class.
Note: CharSequence/String are example parameters of use, my real usage is different.
解决方案
How can I declare inner class to use same generic type of outer class?
Inner
is already using the same generic type as Outer
. The problem is that func
is not using it, it is using CharSequence
.
As SLaks explained func
will do funky stuff in all cases except when it is called on an Outer<CharSequence>
: e.g. when called on Outer<String>
it will add a CharSequence
to an ArrayList<String>
.
Now, if func
already had an object of that concrete type:
void func(E e)
{
Inner in = new Inner();
in.add(e);
}
It would do the right thing. So the compiler allows this.
It seems you tried to create a generic question from some specific issue you are facing. If you described what you are trying to do and failing you will have better chance of resolving it here.
这篇关于Java泛型类,使用外部类的参数的内部类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!