K-DigitalTraining 강의/1. Java
[2주차] 39. 객체로 배열 만들기
이대곤
2022. 5. 25. 18:12
원래 정수의 배열 만들던 방식 + 객체생성하는 방식을 고려하면 쉽게 기억할 수 있다.
코드
class SeasonBook {
String title;
String name;
int price;
SeasonBook() {
this.title = "JSP";
this.name = "써니";
this.price = 1000;
}
SeasonBook(String title, int price) {
this.title = title;
this.name = "임효상";
this.price = price;
}
SeasonBook(String title, String name, int price) {
this.title = title;
this.name = name;
this.price = price;
}
void show() {
System.out.println(title);
System.out.println(name);
System.out.println(price);
}
}
public class Ex05_18_객체배열 {
public static void main(String[] args) {
// SeasonBook b1 = new SeasonBook();
SeasonBook[] bk_arr1 = { new SeasonBook(), new SeasonBook("HTML", "아이유", 10000), new SeasonBook("DB", 20000) };
SeasonBook[] bk_arr2 = new SeasonBook[3];
bk_arr2[0] = new SeasonBook();
bk_arr2[1] = new SeasonBook("CSS", "최광희", 30000);
bk_arr2[2] = new SeasonBook("C#", 40000);
// 반복문으로 추력
for (int i = 0; i < bk_arr1.length; i++) {
bk_arr1[i].show();
System.out.println();
}
// 반복문으로 추력
for (int i = 0; i < bk_arr2.length; i++) {
bk_arr2[i].show();
System.out.println();
}
}
}
실행결과
김세은
50
70
수지
0
0
수영
60
80