์ฑ ์ ์ ๋ชฉ๊ณผ ์๊ฐ์ ๋ํ ์ ๋ณด๋ฅผ ๋ํ๋ด๋ ์ฝ๋๋ฅผ ํตํด this() ํจ์์ ๋ํด ์ดํดํด๋ณด์
์ ์ฒด์ฝ๋
package Chapter04;
public class Book {
String title;
String author;
//show method
void show() {
System.out.println(title + " - " + author);
}
//์์ฑ์1
public Book() {
this("", "");
}
//์์ฑ์2
public Book(String title) {
this(title, "์์๋ฏธ์");
}
//์์ฑ์3
public Book(String title, String author) {
this.title = title;
this.author = author;
}
public static void main(String[] args) {
Book LittlePrince = new Book("์ด๋ฆฐ์์", "์ํ์ฅ๋ฒ ๋ฆฌ"); //๊ฐ์ฒด1
Book LoveStory = new Book("์ถํฅ์ "); //๊ฐ์ฒด2
Book Empty = new Book(); //๊ฐ์ฒด3
LoveStory.show(); //์คํ1
LittlePrince.show(); //์คํ2
Empty.show(); //์คํ3
}
}
์คํ ์์
1. ์คํ1 : LoveStory ๊ฐ์ฒด๋ฅผ show method์ ์คํ์์ผ๋ผ >> ๊ฐ์ฒด2
2. ๊ฐ์ฒด2๋ ๋ณ์๊ฐ 1๊ฐ (title - ์ถํฅ์ ) >> ๋ณ์ 1๊ฐ๋ฅผ ๋ฐ๋ ์์ฑ์2๋ก (title - ์ถํฅ์ author - ์์๋ฏธ์)
3. ์์ฑ์2 ์์ this.()๋ ๋งค๊ฐ๋ณ์๊ฐ 2๊ฐ >> ๋ณ์ 2๊ฐ๋ฅผ ๋ฐ๋ ์์ฑ์3์ผ๋ก
4. ์์ฑ์3์์ title๊ณผ author์ ์ ๋ณด ์ ๋ ฅ
5. show method์ ๋ณ์ ์ ๋ณด๋ฅผ ์ ๋ ฅํด์ ํจ์ ์คํ
>>์ถํฅ์ - ์์๋ฏธ์
6. ์คํ2: LittlePrince ๊ฐ์ฒด๋ฅผ show method์ ์คํ์์ผ๋ผ >> ๊ฐ์ฒด1
7. ๊ฐ์ฒด1์ ๋ณ์๊ฐ 2๊ฐ (title - ์ด๋ฆฐ์์, author - ์ํ์ฅ๋ฒ ๋ฆฌ) >> ๋ณ์ 2๊ฐ๋ฅผ ๋ฐ๋ ์์ฑ์3์ผ๋ก
8. ์์ฑ์3์์ title๊ณผ author์ ์ ๋ณด ์ ๋ ฅ
9. show method์ ๋ณ์ ์ ๋ณด๋ฅผ ์ ๋ ฅํด์ ํจ์ ์คํ
>>์ด๋ฆฐ์์ - ์ํ์ฅ๋ฒ ๋ฆฌ
10. ์คํ3: Empty ๊ฐ์ฒด๋ฅผ show method์ ์คํ์์ผ๋ผ >> ๊ฐ์ฒด3
11. ๊ฐ์ฒด3์ ๋ณ์๊ฐ 0๊ฐ >> ๋ณ์๋ฅผ 0๊ฐ ๋ฐ๋ ์์ฑ์1๋ก (title - , author - )
12. ์์ฑ์1 ์์ this()๋ ๋งค๊ฐ๋ณ์๊ฐ 2๊ฐ >> ๋ณ์๋ฅผ 2๊ฐ ๋ฐ๋ ์์ฑ์3์ผ๋ก
13. ์์ฑ์3์์ title๊ณผ author์ ์ ๋ณด ์ ๋ ฅ
14. show method์ ๋ณ์ ์ ๋ณด๋ฅผ ์ ๋ ฅํด์ ํจ์ ์คํ
>> -
console
์ถํฅ์ - ์์๋ฏธ์
์ด๋ฆฐ์์ - ์ํ์ฅ๋ฒ ๋ฆฌ
-
'๐ฅ๏ธ > Java' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Java] ํ์ ์ ๋ณด๋ฅผ ๋ฐ๋ ๊ธฐ๋ณธ ์ฝ๋ (0) | 2023.03.20 |
---|---|
[Java] ๋ฐฐ์ด: ์์ ์ฝ๋๋ก ๋ฐฐ์ด ์์ฑ, ๋ฐฐ์ด ์์ ๊ฐ์ฒด ์์ฑ, ๋ฐฐ์ด ์ถ๋ ฅํ๋ ๋ฒ ๋ฐฐ์ฐ๊ธฐ (0) | 2023.03.19 |
[Java] ์์ฑ์(Constructor)์ ํน์ง (0) | 2023.03.19 |
[Java] Chapter03: ์ซ์ ๋ง์ถ๊ธฐ ๊ฒ์ (Up&Down) (2) | 2023.03.19 |
[Java] Chapter02: ๊ฐ์๋ฐ์๋ณด ๊ฒ์ (0) | 2023.03.18 |