티스토리 뷰
단순한 클래스 사용 예제 입니다
class J0516_1 {
long number;
String owner;
int point;
int balance;
void use(int amount){
balance += amount;
System.out.println("현재 카드 사용액 : " + balance);
}
void payBill(int amount){
balance -= amount;
System.out.println("지불액: "+amount+", 지불 잔액:"+balance);
addPoint(amount);
}
void addPoint(int amount){
point += amount/1000;
System.out.println("보너스 포인트:" +point);
}
public static void main(String[] args){
J0516_1 j = new J0516_1();
j.number = 3456_7654_8765_5647L;
j.owner = "홍길동";
System.out.print("카드번호 :"+j.number);
System.out.println(",카드소유자 :"+j.owner);
j.use(30000);
j.payBill(10000);
}
}
위 예제에서 get. set 메소드를 추가한 예제입니다.
class J0516_2 {
private long number;
String owner;
private int point;
private int balance;
public long getNumber() {
return number;
}
public void setNumber(long number) {
if(number < 1000_0000_0000_0000L){
System.err.println("잘못된 카드 번호 입니다.");
return;
}
this.number = number;
}
public int getPoint() {
return point;
}
public void setPoint(int point) {
this.point = point;
}
public int getBalance() {
return balance;
}
public void setBalance(int balance) {
this.balance = balance;
}
void use(int amount){
balance += amount;
System.out.println("현재 카드 사용액 : " + balance);
}
void payBill(int amount){
balance -= amount;
System.out.println("지불액: "+amount+", 지불 잔액:"+balance);
addPoint(amount);
}
void addPoint(int amount){
point += amount/1000;
System.out.println("보너스 포인트:" +point);
}
public static void main(String[] args){
J0516_2 j = new J0516_2();
j.number = 3456_7654_8765_5647L;
j.owner = "";
System.out.print("카드번호 :"+j.number);
System.out.println(",카드소유자 :"+j.owner);
j.use(30000);
j.payBill(10000);
}
}
첫번쨰 예제에서 생성자를 추가한 예제입니다.
class J0516_3 {
private long number;
String owner;
private int point;
private int balance;
public J0516_3(String owner){
this.owner = owner;
}
public J0516_3(long number){
this.number = number;
}
public J0516_3(String owner, long number){
this.owner = owner;
this.number = number;
}
public long getNumber() {
return number;
}
public void setNumber(long number) {
if(number < 1000_0000_0000_0000L){
System.err.println("잘못된 카드 번호 입니다.");
return;
}
this.number = number;
}
public int getPoint() {
return point;
}
public void setPoint(int point) {
this.point = point;
}
public int getBalance() {
return balance;
}
public void setBalance(int balance) {
this.balance = balance;
}
void use(int amount){
balance += amount;
System.out.println("현재 카드 사용액 : " + balance);
}
void payBill(int amount){
balance -= amount;
System.out.println("지불액: "+amount+", 지불 잔액:"+balance);
addPoint(amount);
}
void addPoint(int amount){
point += amount/1000;
System.out.println("보너스 포인트:" +point);
}
public static void main(String[] args){
J0516_3 j1 = new J0516_3("권해동");
J0516_3 j2 = new J0516_3(3452_4156_3542_1651L);
j2.owner = "전우치";
J0516_3 j3 = new J0516_3("홍길동", 1651_1651_1232_8641L);
System.out.println("j1: "+ j1.owner + " " + j1.getNumber());
System.out.println("j2: "+ j2.owner + " " + j2.getNumber());
System.out.println("j3: "+ j3.owner + " " + j3.getNumber());
}
}
'IT > Java' 카테고리의 다른 글
[Java] Calendar클래스를 사용한 달력예제 (0) | 2014.05.20 |
---|---|
[Java] 상속예제2 (0) | 2014.05.19 |
[Java] 상속예제 (0) | 2014.05.11 |
[Java] 상속(Inheritance)이란 ? (0) | 2014.05.11 |
[Java] ArrayList란? (2) | 2014.05.07 |