import java.util.ArrayList;
public class Watch{
// instance variables for the class watch
private String name;
private long serial_no;
private String desc;
private String color;
private double price;
private double weight;
private int hour;
private int min;
private int sec;
ArrayList<Watch> watchStore = new ArrayList<Watch>();
public void addWatch(String nme, long s_r, String ds, String cl, double pr, double wg){
watchStore.add(new Watch(nme, s_r, ds, cl, pr, wg));
}
public String disWatch(){
return watchStore.get(watchStore) + "";
}
在我的addWatch
方法中,我创建了一个new Watch
来添加到Watch
类内的ArrayList。这可能吗?
答案 0 :(得分:2)
我想最好有两个类:Store
和Watch
,其中类Store
包含类似ArrayList<Watch> watches;
的列表。
班级Store
:
public class Store {
private ArrayList<Watch> watches;
public Store() {
watches = new ArrayList<>();
// Add first watch
watches.add(new Watch(...));
}
}
班级Watch
:
public class Watch {
private String name;
private long serial_no;
private String desc;
private String color;
private double price;
private double weight;
private int hour;
private int min;
private int sec;
public Watch(...) {
// ...
}
}
答案 1 :(得分:0)
是的,为什么不呢?但要制作更多容器类来存储手表(WatchStore
)。