class Wardrobe { private ArrayList<clothes> clothesList = new ArrayList<>(); //添加的函数 public void addClothes(clothes clothes) { //提供了添加方法 //该方法创建了衣物 clothesList.add(clothes); } //删除的函数 public void removeClothes(int index) { //提供了删除方法 clothesList.remove(index); } //查看的函数 public void listClothes() { //添加默认衣服 Scanner scanner=new Scanner(System.in); clothes newClothes = new clothes("默认衣物", "黑色", 38); addClothes(newClothes); //提供了查看方法 //通过进行for循环来查看多件衣服衣服 //for循环的实现 for (int i = 0; i < clothesList.size(); i++) { //获取衣服的属性 clothes clothes =clothesList.get(i); //输出衣服 System.out.println((i + 1) + ". " + clothes.getName() + " (" + clothes.getColor() + ", " + clothes.getSize() ); } }代码修改
时间: 2024-02-23 17:01:24 浏览: 16
根据你的需求,可以将`addClothes()`函数和默认衣物的添加移到`Wardrobe`类的构造函数中,这样每次创建`Wardrobe`对象时就会自动添加一个默认衣物。同时,修改`listClothes()`函数,去掉默认衣物的添加,只输出`clothesList`中的衣物信息。修改后的代码如下:
```
class Wardrobe {
private ArrayList<clothes> clothesList = new ArrayList<>();
public Wardrobe() {
// 添加默认衣物
clothes newClothes = new clothes("默认衣物", "黑色", 38);
addClothes(newClothes);
}
// 添加的函数
public void addClothes(clothes clothes) {
// 提供了添加方法
// 该方法创建了衣物
clothesList.add(clothes);
}
// 删除的函数
public void removeClothes(int index) {
// 提供了删除方法
clothesList.remove(index);
}
// 查看的函数
public void listClothes() {
// 提供了查看方法
// 通过进行for循环来查看多件衣服衣服
// for循环的实现
for (int i = 0; i < clothesList.size(); i++) {
// 获取衣服的属性
clothes clothes = clothesList.get(i);
// 输出衣服
System.out.println((i + 1) + ". " + clothes.getName() + " (" + clothes.getColor() + ", " + clothes.getSize() );
}
}
}
```