public class Company {
private int id;
private static int serial = 10001;
private static Company instance = new Company();
public static Company getInstance() {
return instance;
}
public Card createCard() {
Card card = new Card(serial++);
return card;
}
private Company() {
}
}
----
public class Card {
private int cardnum;
public Card(int serial) {
cardnum = serial;
}
public int getCardNumber() {
return cardnum;
}
}
----
public class Ts {
public static void main(String[] args) {
// TODO Auto-generated method stub
Company company = Company.getInstance();
Card myCard = company.createCard();
Card yourCard = company.createCard();
System.out.println(myCard.getCardNumber());
System.out.println(yourCard.getCardNumber());
}
}
'Java' 카테고리의 다른 글
up casting & override (0) | 2019.10.27 |
---|---|
상속 (0) | 2019.10.27 |
자바 연습 (Array List) (0) | 2019.10.26 |
다차원 배열 (0) | 2019.10.26 |
깊은 복사 & 얕은 복사. (0) | 2019.10.26 |