์ฐ์ต๋ฌธ์ Q6) ์นด๋ ํ์ฌ์์ ์นด๋๋ฅผ ๋ฐ๊ธํ ๋๋ง๋ค ์นด๋ ๊ณ ์ ๋ฒํธ๋ฅผ ๋ถ์ฌํด ์ค๋ค.
06-3์ <ํ๋ฒ ์์ฑํ๊ธฐ> ์์ ๋ฅผ ์ฐธ๊ณ ํ์ฌ ์นด๋๊ฐ ์์ฑ๋ ๋๋ง๋ค ์นด๋ ๋ฒํธ๊ฐ ์๋์ผ๋ก ์ฆ๊ฐํ ์ ์๋๋ก ์นด๋ ํด๋์ค๋ฅผ ๋ง๋ค๊ณ ์์ฑํด๋ณด์.
โ
โ
<๋ด๊ฐ ํ ๊ฒ>
package singleton;
public class Card {
private static int serialNum = 100;
String cardName;
int cardNum;
public Card() {
serialNum++;
cardNum = serialNum;
}
public Card(String card) {
serialNum++;
cardNum = serialNum;
this.cardName = card;
}
public int getCardNum() {
return cardNum;
}
}
package singleton;
public class CardTest {
public static void main(String[] args) {
Card card1 = new Card();
Card card2 = new Card();
Card card3 = new Card("BC");
System.out.println(card1.cardName + " ์นด๋๋ฒํธ : " + card1.getCardNum());
System.out.println(card2.cardName + " ์นด๋๋ฒํธ : " + card2.getCardNum());
System.out.println(card3.cardName + " ์นด๋๋ฒํธ : " + card3.getCardNum());
}
}
<ํท๊ฐ๋ ธ๋ ๊ฒ>
<๊ฒฐ๊ณผ๊ฐ>
โ<์ ์๋์ด ํ ๊ฒ>
package singleton;
public class Card {
private static int serialNum = 100;
String cardName;
String cardNum;
public Card() {
serialNum++;
cardNum = String.valueOf(serialNum);
}
public Card(String cardName) {
this();
this.cardName = cardName;
}
public String getCardNum() {
return cardNum;
}
}
package singleton;
public class CardTest {
public static void main(String[] args) {
Card card1 = new Card();
Card card2 = new Card();
Card card3 = new Card("BC");
System.out.println(card1.cardName + " ์นด๋๋ฒํธ : " + card1.getCardNum());
System.out.println(card2.cardName + " ์นด๋๋ฒํธ : " + card2.getCardNum());
System.out.println(card3.cardName + " ์นด๋๋ฒํธ : " + card3.getCardNum());
}
}
์ฐ์ต๋ฌธ์ Q7) Q6์ ๊ตฌํํ ๋ด์ฉ์ ์นด๋ ํ์ฌ ํด๋์ค CardCompany๋ฅผ ์ฑ๊ธํค ํจํด์ ์ฌ์ฉํ์ฌ ๊ตฌํํด ๋ณด๋ผ.
package singleton;
public class CardCompany {
private static CardCompany instance;
private CardCompany() {
}
public static CardCompany getInstance() {
if (instance == null) {
instance = new CardCompany();
}
return instance;
}
public Card createCard() {
return new Card();
}
public Card createCard(String cardName) {
return new Card(cardName);
}
}
package singleton;
public class CardCompanyTest {
public static void main(String[] args) {
CardCompany company = CardCompany.getInstance();
Company myBC = Company.createCard();
Company myVisa = Company.createCard("Visa");
System.out.println(myBC.cardName + " " + myBC.getCardNum());
System.out.println(myVisa.cardName + " " + myVisa.getCardNum());
}
}
<๊ฒฐ๊ณผ๊ฐ>
โ
์ฌํ๋ฌธ์ ) member ๊ฐ์ฒด ์์ฑ์ ์ฑ(last name)๊ณผ ์ด๋ฆ์ ๋งค๊ฐ ๋ณ์๋ก ๋ฐ๊ณ
memberID๊ฐ์ lastname์ด ํฌํจ๋๊ฒ ์๋ ํ ๋น - ๋งค๊ฐ ๋ณ์ ๊ฐ์ด ์์ผ๋ฉด ์๋๋ค!
ํด๋์ค : Member ๋ฉค๋ฒ ๋ณ์ : memberID, memberName, lastName - get, set ํจ์ |
-์ฃผ๋ชฉ! ์ด๋ผ๊ณ ํ์ํ ๋ถ๋ถ๊ณผ Member ํด๋์ค์ ๋ ธ๋ ํ๊ดํ๋ถ๋ถ์ ์ ์ดํด๋ณด์.
โ
โ
โ
์ฌํ๋ฌธ์ ) site ํด๋์ค
๋ฉค๋ฒ ๋ณ์ : siteCode, siteName ํด๋์ค ๋ณ์ : 1. siteCode ๊ฐ์ ์๋ ์ฆ๊ฐ ํ ๋นํ๋ ๋ณ์ 2. ์ฑ๊ธํค์ ์ํ ๋ณ์ ์ฑ๊ธํค์ผ๋ก ๊ฐ์ฒด๊ฐ ์์ฑ๋๊ฒ ์์ฑ์ ํ๊ณ ๊ฐ์ฒด ์์ฑ ์ site์ด๋ฆ์ ์ ๋ ฅ๋ฐ์ ์ ์ฅํ ์์๊ฒ ํ ๊ฒ! |
โ
-๋นจ๊ฐ์ ๋ฐ์ค ์น ๋ถ๋ถ์ด ํท๊ฐ๋ ธ๋ค.
'Java' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Java] ๋ฐฐ์ด ๋ณต์ฌํ๊ธฐ (0) | 2021.02.19 |
---|---|
[Java] ๋ฐฐ์ด (0) | 2021.02.19 |
[Java] static ์์ฉ - ์ฑ๊ธํค ํจํด (0) | 2021.02.19 |
[Java] ๋ณ์ ์ ํจ ๋ฒ์ (0) | 2021.02.19 |
[Java] ํด๋์ค ๋ฉ์๋ (0) | 2021.02.18 |
๋๊ธ