-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFoodTest.java
More file actions
35 lines (29 loc) · 930 Bytes
/
Copy pathFoodTest.java
File metadata and controls
35 lines (29 loc) · 930 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package javaExD;
public class FoodTest {
public static void main(String[] args) {
// 음식 객체 생성
Food chicken = new Food("치킨", 18000);
Food pizza = new Food("피자", 28000);
Food sushi = new Food("초밥세트", 22000);
// 객체 배열에 음식 담기
Food[] foods = { chicken, pizza, sushi };
// 모든 음식 정보 출력
for (int i = 0; i < foods.length; i++) {
System.out.println(foods[i].toString());
}
}
}
class Food {
/* 1. 필드를 추가하세요. */
String name;
int price;
/* 2. 생성자를 추가하세요. */
Food(String name, int price) {
this.name = name;
this.price = price;
}
/* 3. toString() 메소드를 추가하세요. */
public String toString() {
return String.format("Food { name: %s, price: %d원 }", this.name, this.price);
}
}