只是寻找以下内容的明确定义:
当您包含 ArrayList<Product>
时在类声明中,product 是另一个包含一些实例变量的类。当使用如下所示的代码时,是ArrayList
在创建对象时初始化?
例如:
public class Basket extends ArrayList<Product> implements Serializable
{
private int theOrderNum = 0; // Order number
public Basket()
{
theOrderNum = 0;
}
}
如果我创建新类的实例:
Basket test = new Basket();
这会在正在创建的对象上创建一个数组列表吗?为了访问ArrayList
在对象内部,我可以使用 this.add(foo);
?
最佳答案
扩展ArrayList的类,拥有ArrayList的所有方法。实例化此类将创建一个 ArrayList,因为 Basket 是一个 ArrayList。您可以在 test
实例上调用 ArrayList 的所有公共(public)方法。
test.add(foo)
(以及 Basket 类的非静态方法中的 this.add(foo)
)可以工作。
关于java - 类声明中的 ArrayList<Product> 如何访问 ArrayList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26530524/