java/코드 리뷰
9. oop4 - (1) BookDemo (생성자 메서드)
Astaroth아스
2020. 3. 24. 17:05
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
package oop4;
public class Book {
String title;
String writer;
String publisher;
int price;
String genre;
// 생성자 메서드
// 기본 생성자 메서드(default constructor)
// 클래스에 정의된 생성자가 하나도 없을 때 컴파일러가 자동으로 추가하는 생성자 메서드
Book() {
publisher = "한빛미디어"; // 대부분 책이 한빛미디어 출판이기 때문에 Book 객체 생성 후에 publisher를 한빛미디어로 설정
}
// 매개변수가 있는 생성자 메서드
// 생성자 메서드 중복정의
Book(String inputTitle, String inputWriter, int inputPrice, String inputGenre) {
title = inputTitle;
writer = inputWriter;
publisher = "한빛미디어";
price = inputPrice;
genre = inputGenre;
}
Book(String inputTitle, String inputWriter, String inputPublisher, int inputPrice, String inputGenre) {
title = inputTitle;
writer = inputWriter;
publisher = inputPublisher;
price = inputPrice;
genre = inputGenre;
}
// 일반 멤버 메서드(인스턴스 메서드)
void printBookInfo() {
System.out.println("ㅡㅡㅡㅡㅡ 도서 정보 ㅡㅡㅡㅡㅡ");
System.out.println("제목 : " + title);
System.out.println("저자 : " + writer);
System.out.println("출판사 : " + publisher);
System.out.println("가격 : " + price);
System.out.println("장르 : " + genre);
System.out.println("ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ");
System.out.println();
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
package oop4;
public class BookDemo {
public static void main(String[] args) {
Book book1 = new Book();
book1.title = "자바의 정석";
book1.writer = "남궁성";
book1.price = 30000;
book1.genre = "기술/컴퓨터";
book1.printBookInfo();
Book book2 = new Book("처음 만나는 머신러닝", "오다카 토모히로", 18000, "기술/컴퓨터");
book2.printBookInfo();
Book book3 = new Book("파이썬 실전 프로그래밍", "최진식", "좋은땅", 25600, "기술/컴퓨터");
book3.printBookInfo();
}
}
|