【发布时间】:2012-09-30 03:22:14
【问题描述】:
我需要帮助解决 Java 作业问题。我有两个包,比如说bag1,其中包含字符串A、B、C 和D 和bag2,其中包含字符串E、F、G 和@987654330我需要为这两个包的联合编写一个 BagInterface,然后是一个类调用ArrayBag<T> implements BagInterface<T>。
BagInterface 我在想这样的事情:
public interface BagInterface<T> {
public T union(T[] item);
}
public class ArrayBag<T> implements BagInterface<T> {
private final static int DEFAULT_CAP = 4;
private int numElements;
private T[] bag;
public ArrayBagR(int cap) {
bag = (T[]) new Object[cap];
this.numElements = 0;
}
public T union(T[] item) {
// Not sure how I should write this so I can pass
// another class object in the parameter
// Like say if I write a main to run this I could
// do something like Bag1.union(Bag2)
// and get something like A B C D E F G H
}
}
如果我有这个就说吧
public static void main(String[] args) {
BagInterface bag1 = new ArrayBag(n);
BagInterface bag2 = new ArrayBag(m);
BagInterface<String> everything = bag1.union(bag2);
}
【问题讨论】:
-
union是做什么的?它会创建一个新的BagInterface实例吗?它是否执行参数与当前包的联合? -
是的,它应该创建一个新的 BagInterface 实例并将两个当前包的字符串添加到新包中。
标签: java object interface union bag