Java generic class, inner class using parameter of outer class

帅比萌擦擦* 提交于 2020-01-14 05:53:46

问题


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.


回答1:


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.




回答2:


That is fundamentally unsafe.

What would happen if I make an Outer<StringBuilder>?
You would end up adding a String to an 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).




回答3:


 class Inner extends ArrayList<CharSequence>{
 }


来源:https://stackoverflow.com/questions/13404331/java-generic-class-inner-class-using-parameter-of-outer-class

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!