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

반응형

+ Recent posts