2011년7월29일_JAVA 상속, 생성자, 오버로딩, 오버라이딩 예제와 OOP의 장점
시간 뉴뉴
참조(Reference) |
1: //AccountTest1.java (main)
2: package kr.ac.busanit;
3:
4: public class AccountTest {
5:
6: /**
7: * @param args
8: */
9: public static void main(String[] args) {
10: // TODO Auto-generated method stub
11: //CheckingAccount check = new CheckingAccount("111-222-333333", "김수만", 0, "3333-4444-5555-6666", 100);
12: CheckingAccount check = new CheckingAccount();
13:
14: check.accountNo = "111-222-333333";
15: check.ownerName = "김수만";
16: check.checkNo = "3333-4444-5555-6666";
17: System.out.println("계좌번호: " + check.getaccountNo());
18: System.out.println("체크카드번호: " + check.getCheckNo());
19: System.out.println("계좌주: " + check.getName());
20: System.out.println("잔액은 " + check.deposit(1000000) + "원 입니다."); //100만원입금
21:
22: if(0 < check.withdraw(200000)) { //20만원출금
23: System.out.println("잔액은 " + check.getBalance() + "원 입니다."); //잔고확인
24: }
25: else { //예외처리
26: System.out.println("잔액이 부족합니다. ㅋㅋ");
27: }
28:
29: //check.balance = 100000000;
30: check.pay("3333-4444-5555-6666", 300000); //30만원지불
31: System.out.println("잔액은 " + check.getBalance() + "원 입니다."); //잔고확인
32: }
33: }
1: //Account.java
2: package kr.ac.busanit;
3:
4: public class Account {
5: //멤버변수
6: protected String accountNo; //계좌번호
7: protected String ownerName; //계좌주
8: protected int balance; //잔고
9:
10: //생성자
11: Account () {
12:
13: }
14: Account (String accountNo, String name, int balance) {
15: this.accountNo = accountNo;
16: this.ownerName = name;
17: this.balance = balance;
18: }
19:
20: //getter/setter
21: public String getaccountNo() {
22: return accountNo;
23: }
24: public void setNumber(String accountNo) {
25: this.accountNo = accountNo;
26: }
27: public String getName() {
28: return ownerName;
29: }
30: public void setName(String ownerName) {
31: this.ownerName = ownerName;
32: }
33: public int getBalance() {
34: return balance;
35: }
36: public void setBalance(int balance) {
37: this.balance = balance;
38: }
39:
40: //입금메소드
41: public int deposit(int amount) {
42: this.balance = this.balance + amount;
43: return balance; //잔고리턴
44: }
45:
46: //출금메소드
47: public int withdraw(int amount) {
48: if(balance >= amount) { //잔고보다 작은 경우만 허용
49: balance = balance - amount;
50: }
51: else {
52: return -1; //에러값으로 -1을 리턴
53: }
54:
55: return balance;
56: }
57: }
1: //CheckingAccount.java
2: package kr.ac.busanit;
3:
4: public class CheckingAccount extends Account {
5: protected String checkNo;
6: protected int TOPpoint;
7:
8: //생성자
9: CheckingAccount () {
10:
11: }
12:
13: CheckingAccount (String accountNo, String ownerName, int balance, String checkNo, int TOPpoint) {
14: super(accountNo, ownerName, balance);
15: this.checkNo = checkNo;
16: this.TOPpoint = TOPpoint;
17: }
18:
19: //getter/setter
20: public String getCheckNo() {
21: return checkNo;
22: }
23:
24: public void setCheckNo(String checkNo) {
25: this.checkNo = checkNo;
26: }
27:
28: public int getTOPpoint() {
29: return TOPpoint;
30: }
31:
32: public void setTOPpoint(int tOPpoint) {
33: TOPpoint = tOPpoint;
34: }
35:
36: //지불메소드
37: public int pay(String checkNo, int amount) {
38: if(true == (this.balance > amount) && true == (this.checkNo.equals(checkNo))) {
39: this.balance -= amount;
40: }
41: else {
42: System.out.println("잔액이 부족합니다. ㅋㅋ");
43: }
44:
45: return balance; //성공시 잔고반환
46: }
47: }
● InheritanceExample1 프로젝트
1: //InheritanceExample1.java
2: package kr.ac.busanit;
3:
4: public class InheritanceExample1 {
5:
6: /**
7: * @param args
8: */
9: public static void main(String[] args) {
10: // TODO Auto-generated method stub
11: CheckingAccount obj = new CheckingAccount();
12: obj.setAccountNo("111-22-33333333");
13: obj.setOwnerName("홍길동");
14: obj.setCardNo("5555-6666-7777-8888");
15: obj.deposit(100000);
16: try {
17: int paidAmount = obj.pay("5555-6666-7777-8888", 47000);
18: System.out.println("지불액:" + paidAmount);
19: System.out.println("잔액:" + obj.getBalance());
20: }
21: catch (Exception e) {
22: String msg = e.getMessage();
23: System.out.println(msg);
24: }
25:
26: }
27:
28: }
1: //Account.java
2: package kr.ac.busanit;
3:
4: public class Account {
5: //멤버변수
6: private String accountNo;
7: private String ownerName;
8: private int balance;
9:
10: public String getAccountNo() {
11: return accountNo;
12: }
13:
14: public void setAccountNo(String accountNo) {
15: this.accountNo = accountNo;
16: }
17:
18: public String getOwnerName() {
19: return ownerName;
20: }
21:
22: public void setOwnerName(String ownerName) {
23: this.ownerName = ownerName;
24: }
25:
26: public int getBalance() {
27: return balance;
28: }
29:
30: public void setBalance(int balance) {
31: this.balance = balance;
32: }
33:
34: //멤버메소드
35: public void deposit(int amount) {
36: balance += amount;
37: }
38:
39: public int withdraw(int amount) throws Exception {
40: if (balance < amount)
41: throw new Exception("잔액이 부족합니다.");
42: balance -= amount;
43: return amount;
44: }
45: }
1: //CheckingAccount.java
2: package kr.ac.busanit;
3:
4: public class CheckingAccount extends Account {
5: private String cardNo;
6:
7: public String getCardNo() {
8: return cardNo;
9: }
10:
11: public void setCardNo(String cardNo) {
12: this.cardNo = cardNo;
13: }
14:
15: int pay(String cardNo, int amount) throws Exception {
16: if (!cardNo.equals(this.cardNo) || (this.getBalance() < amount))
17: throw new Exception("지불이 불가능합니다.");
18: return withdraw(amount);
19: }
20:
21: }
<실행결과>