定义一个Dish类型ArrayList
时间: 2024-01-25 14:03:13 浏览: 106
好的,这是一个Java编程问题, 以下是示例代码:
```java
import java.util.ArrayList;
public class Dish {
private String name;
private String description;
public Dish(String name, String description) {
this.name = name;
this.description = description;
}
public String getName() {
return name;
}
public String getDescription() {
return description;
}
public void setName(String name) {
this.name = name;
}
public void setDescription(String description) {
this.description = description;
}
public String toString() {
return name + " - " + description;
}
}
public class Main {
public static void main(String[] args) {
ArrayList<Dish> dishes = new ArrayList<Dish>();
dishes.add(new Dish("Pasta", "Italian pasta with tomato sauce"));
dishes.add(new Dish("Sushi", "Japanese food with raw fish"));
dishes.add(new Dish("Hamburger", "American fast food with beef and bun"));
for (Dish dish : dishes) {
System.out.println(dish.toString());
}
}
}
```
在这个示例代码中,我们定义了一个Dish类来描述菜肴,包括菜名和描述信息。我们使用ArrayList进行管理,并添加一些示例菜肴。最后,我们使用增强型for循环迭代ArrayList以输出每个Dish对象的详细信息。
阅读全文