T>延伸自ArrayList的&LT T舱的使用方法; [英] Use method from T class extending ArrayList<T>

查看:160
本文介绍了T>延伸自ArrayList的&LT T舱的使用方法;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不知道如果标题是有道理的,我会尽量解释。我有延长的ArrayList,一个CustomList T与假设类A1和A2级,既延长A级这是一个自定义类。

Not sure if the title makes sense, I will try to explain. I have a CustomList extending ArrayList, T being let's say Class A1 and Class A2, both extending Class A which is a custom Class.

我需要这样做:

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() A类方法,以及使用这种CustomList只为 A1 A2 ,我敢肯定, obj.getCustomBoolean()荣获' ŧ导致异常。

The method getCustomBoolean() is a Class A method, and using this CustomList only for A1 and A2, I'm sure obj.getCustomBoolean() won't cause an exception.

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

I need a way to specify that T is a child class of A.

推荐答案

类的第一行改成这样。

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

这指定 T 可以是任何类型,是 A 的子类型。它可以让你使用 A 你的 T 对象类型的方法,你的类code之内。

That specifies that T can be any type which is a subtype of A. It will allow you to use methods of type A on your T object, within your class's code.

这篇关于T&GT;延伸自ArrayList的&LT T舱的使用方法;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆