【发布时间】:2015-12-04 05:50:05
【问题描述】:
这就是我的类定义的方式:
public class ArrayBag implements Bag {
/**
* The array used to store the items in the bag.
*/
private Object[] items;
/**
* The number of items in the bag.
*/
private int numItems;
...等等...
这是类定义中的一个方法,这个类的新对象是在方法内部创建的:
//creates and returns an Arraybag that is the union of the called Arraybag and the parameter bag
public bag unionWith(Bag other) {
if (other == null)
throw new IllegalArgumentException();
int cap = this.capacity() + other.capacity();
//new object created here
ArrayBag newbag = new ArrayBag(cap);
for (int i = 0; i < numItems; i++) {
if (other.contains(items[i]))
newbag.add(items[i]);
}
for (int i = 0; i < newbag.numItems(); i++)
//Can I use "newbag.items[i]"?
if (numOccur(newbag.items[i]))
}
我的问题是,我可以从这个方法定义中访问 newbag 对象的 Object[] 项吗?像这样:newbag.items[i]
【问题讨论】:
-
为什么不测试一下自己呢?您已经编写了示例。
标签: java inheritance private