【问题标题】:Use method from T class extending ArrayList<T>使用 T 类扩展 ArrayList<T> 的方法
【发布时间】:2014-06-07 23:55:12
【问题描述】:

不确定标题是否有意义,我将尝试解释。我有一个扩展 ArrayList 的 CustomList,假设 T 是 A1 类和 A2 类,它们都扩展了作为自定义类的 A 类。

我需要这样做:

public class CustomList<T> extends ArrayList<T>
{
    public CustomList()
    {
        super();
    }

    public boolean add(T obj)
    {
        /*The method getCustomBoolean() is not defined for the type T*/
        if(obj.getCustomBoolean())
        {
            super.add(obj);
        }

        return true;
    }
}

getCustomBoolean() 方法是一个Class A 方法,并且仅将这个CustomList 用于A1A2,我确信obj.getCustomBoolean() 不会导致异常。

我需要一种方法来指定 T 是 A 的子类。

【问题讨论】:

    标签: java android arraylist extends


    【解决方案1】:

    把你的类的第一行改成这个。

    public class CustomList<T extends A> extends ArrayList<T>
    

    这指定T 可以是A 的子类型的任何类型。它将允许您在您的类代码中对您的T 对象使用A 类型的方法。

    【讨论】:

      【解决方案2】:

      如果您只将此 CustomList 与 A 的子类一起使用,则可以将 CustomList 类声明为:

      public class CustomList<T extends ClassA> extends ArrayList<T>
      

      但如果不是,那么您必须重新考虑您的设计。

      【讨论】:

        【解决方案3】:

        这样做:

        class A
        {
            public boolean getCustomBoolean() {
                return false;
            }
        }
        
        class CustomList<T extends A> extends ArrayList<T>
        {
            private static final long serialVersionUID = 1L;
        
            public CustomList()
            {
                super();
            }
        
            public boolean add(T obj)
            {
                if(obj.getCustomBoolean())
                {
                    super.add(obj);
                }
        
                return true;
            }
        }
        

        【讨论】:

          【解决方案4】:

          用途:

          class CustomList<T extends A> extends ArrayList<A>
          

          【讨论】:

            【解决方案5】:

            这很好用:

            class A {
                public boolean getCustomBoolean () {
                    return true;
                }
            };
            class A1 extends A {};
            class A2 extends A {};
            
            class CustomList<T extends A> extends ArrayList<A> {
                public boolean add (T obj) {
                    if ( obj.getCustomBoolean() ) {
                        super.add(obj);
                    }
                    return true;
                }
            }
            

            请注意,如果您扩展 ArrayList&lt;A&gt;,您可以添加 A1 类型的项目 A2,但如果您扩展 ArrayList&lt;T&gt;,您将被限制为声明中的类型。

                CustomList<A1> a1 = new CustomList<>();
                CustomList<A2> a2 = new CustomList<>();
                // Fine.
                a1.add(new A1());
                // Fine if you extend ArrayList<A> - not allowed if you extend ArrayList<T>.
                a2.add(new A1());
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2011-10-01
              • 2016-05-08
              • 1970-01-01
              • 2013-04-08
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多