简体   繁体   English

Java泛型类,使用外部类的参数的内部类

[英]Java generic class, inner class using parameter of outer class

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. 注意:CharSequence / String是使用的示例参数,我的实际用法有所不同。

That is fundamentally unsafe. 从根本上讲这是不安全的。

What would happen if I make an Outer<StringBuilder> ? 如果我制作Outer<StringBuilder>会发生什么?
You would end up adding a String to an ArrayList<StringBuffer> . 您最终将向String添加一个ArrayList<StringBuffer>

If you have a collection of <E extends CharSequence> , the only type-safe thing you can put in that collection is an instance of E (or a class that extends E , or null ). 如果您有一个<E extends CharSequence>的集合,则可以放入该集合的唯一类型安全的东西是E的实例(或扩展E的类,或者为null )。

How can I declare inner class to use same generic type of outer class? 如何声明内部类使用与外部类相同的泛型?

Inner is already using the same generic type as Outer . Inner已经使用与Outer相同的通用类型。 The problem is that func is not using it, it is using CharSequence . 问题在于func未使用它,而是在使用CharSequence

As SLaks explained func will do funky stuff in all cases except when it is called on an Outer<CharSequence> : eg when called on Outer<String> it will add a CharSequence to an ArrayList<String> . 正如SLaks 解释的那样, func在所有情况下都会做时髦的事情,除非在Outer<CharSequence>上调用它:例如,在Outer<String>上调用时,它将为ArrayList<String>添加一个CharSequence

Now, if func already had an object of that concrete type: 现在,如果func 已经具有该具体类型的对象:

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. 如果您描述了您要做什么而失败了,您将有更多机会在这里解决它。

 class Inner extends ArrayList<CharSequence>{
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM