简体   繁体   English

在扩展通用类的类中启动实例变量

[英]Initiating instance variables in a class that extends a generic class

I have a generic class "SimpleList" (excerpt): 我有一个通用类“ SimpleList”(节选):

public abstract class SimpleList<T> {

   protected List<T> list;

   public SimpleList(List<T> list) {
      this.list = list;
   }
}

And another class "TrackList" that extends it (excerpt): 还有另一个扩展它的类“ TrackList”(节选):

public class TrackList extends SimpleList {

    public TrackList(List<XmlTrack> list) {
        super(list);
    }
}

In "TrackList" I specify that the list is to hold objects of type "XmlTrack". 在“ TrackList”中,我指定列表将保存“ XmlTrack”类型的对象。 It seems though, like it's not possible to get an object from that list and access its methods. 但是,似乎不可能从该列表中获取对象并访问其方法。 For example, this won't work: 例如,这将不起作用:

list.get(0).someMethodSpecificToXmlTrack() list.get(0).someMethodSpecificToXmlTrack()

I don't understand why this doesn't work? 我不明白为什么这行不通? Isn't the list in "SimpleList" set to only hold "XmlTrack"s? 是不是将“ SimpleList”中的列表设置为仅容纳“ XmlTrack”?

You need to define it as 您需要将其定义为

public class TrackList extends SimpleList<XmlTrack> {

    public TrackList(List<XmlTrack> list) {
        super(list);
    }
}

Because SimpleList is generic, but you did not specify a type argument when you extended it. 因为SimpleList是通用的,但在扩展它时未指定类型参数。

May be you need 可能你需要

public class TrackList extends SimpleList< XmlTrack > 公共类TrackList扩展SimpleList < XmlTrack >

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

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