728x90
300x250
[Java] 상속에 관한 방법

 

상속 방법 1)

 

class Account{
     String accountNo;
     String ownerName;
     int balance;
  
     void deposit(int amount){
          balance += amount;
     }
 
     int withdraw(int amount) throws Exception{
  
     if ( balance < amount )
           throw new Exception("잔액이 부족합니다.");
     else
           balance -= amount;
  
     return amount;
     }

 

Account.java

 

class CheckingAccount extends Account{

      String cardNo;
 
      int pay ( String cardNo, int amount ) throws Exception{
      if ( !cardNo.equals(this.cardNo) || (this.balance < amount ) )
           throw new Exception("지불이 불가능합니다.");
      return withdraw(amount);
     

      }

 

CheckingAccount.java

 

class InheritanceExample{                                                                              
                                                                                                    
      public static void main(String args[]) 

     {                                                            

          CheckingAccount obj;

          obj = new CheckingAccount();                                                   
          obj.accountNo = "110-22-333333";                                                          
          obj.ownerName = "홍길동";                                                                     
          obj.cardNo = "5555-6666-7777-8888";                 
          obj.deposit(100000);                                                                           
                                                                                                 
          try{                                                                                           

                 int paidAmount = obj.pay("5555-6666-7777-8888", 47000);
                 System.out.println("지불액:" + paidAmount);                          
                 System.out.println("잔액:" + obj.balance);                 
                                                                                            
          } catch(Exception e)                                                                           
          {                                                                                             
                  System.out.println(e.getMessage());            

 

           }              
                             
      }                                                                                                                                               
}                                                                                                     

 

InheritanceExample.java

 

상속 방법 2)

 

class Account{
      String accountNo;
      String ownerName;
      int balance;
  
      void deposit(int amount){
            balance += amount;
      }
 
      int withdraw(int amount) throws Exception{
  
             if (balance < amount)
                   throw new Exception("잔액이 부족합니다.");
             else
                   balance -= amount;
  
             return amount;

       }
 

 

 

Account.java

 

class CheckingAccount extends Account{

       String cardNo;

       CheckingAccount(String accountNo, String ownerName, int balance, String cardNo){
           this.accountNo = accountNo;
           this.ownerName = ownerName;
           this.balance = balance;
           this.cardNo = cardNo;

      }
 
       int pay(String cardNo, int amount) throws Exception{

 

       if ( !cardNo.equals(this.cardNo) || (this.balance < amount))
                  throw new Exception("지불이 불가능합니다.");
  

       return withdraw(amount);

 

  }
 

 

CheckingAccount.java

 

class InheritanceExample{                                                                                                                                                                                                                                          
     public static void main(String args[])

    {                                                                             
        CheckingAccount obj;

        obj = new CheckingAccount("113-333-333333", "홍길동", 100000, "5555-6666-7777-8888");
                                                                                              
       try {
             int paidAmount = obj.pay("5555-6666-7777-8888",47000);
             System.out.println("지불액:" + paidAmount);
             System.out.println("잔액:" + obj.balance);
       } catch(Exception e) {
             System.out.println(e.getMessage());

       } 
   }
}

 

InheritanceExample.java

반응형
728x90
300x250

[Java] Class의 Exception 처리

 

Case 1) Numbers(클래스)

 

 

arr[] 공간의 길이가 0이기 때문에 해당 코드는 Exception이 발생하게 됩니다.

Arithmetric Exception이 발생하게 되는데,

 

try{

int average = obj.getAverage();

}catch(java.lang.ArithmeticException e){

     // 출력(산술 오류에 관한 메시지)

}

 

이처럼 처리하는 것도 하나의 방안이 될 수 있으나, 가장 좋은 구조는 Arithmetic Exception이 발생하지 않도록 처리하는 것이 가장 좋은 구조가 아닐까 생각해봅니다.

 

Case 2) Account(클래스)

 

class Account{
        String accountNo;
        String ownerName;
        int balance;
 
        Account(String accountNo, String ownerName, int balance){
             this.accountNo = accountNo;
             this.ownerName = ownerName;
             this.balance = balance;
        }
 
       void deposit(int amount){
             balance += amount;
        }
 
        int withdraw(int amount) throws Exception{
  
            if(balance < amount)
                  throw new Exception("잔액이 부족합니다.");
            else
                  balance -= amount;
  
            return amount;
       }
}

 

 

 

1. Try ~ Catch로 예외 처리

 

class Hello{
 
        public static void main(String args[]){
   
            Account obj = new Account("1", "곰", 40);
            obj.deposit(-40);
  
            try {
                 int amount = obj.withdraw(1);
                 System.out.println("인출액:" + amount);
            } catch (Exception e) {
                 System.out.println(e.getMessage());
            }
       }
}

 

2. throws Exception으로 처리 

 

class Hello{
 
      public static void main(String args[]) throws Exception{
  
            Account obj = new Account("1", "곰", 40);
            obj.deposit(-40);
 
            int amount = obj.withdraw(1);
            System.out.println("인출액:" + amount);
     }
}

 

참고 문헌) 뇌를 자극하는 JAVA 프로그래밍 P238~244

반응형

+ Recent posts