Overriding


  • Overriding(μ˜€λ²„λΌμ΄λ”©)
    • μƒμœ„ν΄λž˜μŠ€μ— μ •μ˜λœ λ©”μ„œλ“œμ˜ κ΅¬ν˜„ λ‚΄μš©μ΄ ν•˜μœ„ ν΄λž˜μŠ€μ—μ„œ κ΅¬ν˜„ν•  λ‚΄μš©κ³Ό λ§žμ§€ μ•Šμ„ 경우 ν•˜μœ„ν΄λž˜μŠ€μ—μ„œ λ™μΌν•œ μ΄λ¦„μ˜ λ©”μ„œλ“œλ₯Ό μž¬μ •μ˜

    • @overriding annotation = μž¬μ •μ˜λœ λ©”μ„œλ“œ


    • ex)

      • Customer.java

          public class Customer {
                        
              protected String customerName;
              protected String customerGrade;
              int bonusPoint;
              double bonusRatio;	//포인트 적립λ₯ 
                        
              public Customer(String customerName) {
                  this.customerName = customerName;
                            
                  customerGrade = "SILVER";
                  bonusRatio = 0.01;
              }
                        
              public int calcPrice(int price) {
                  bonusPoint += price * bonusRatio;
                  //ꡬ맀 κ°€κ²©μ˜ *0.01 만큼 포인트 적립
                  return price;
              }
                        
              public String showCustomerInfo() {
                  return customerName + "λ‹˜μ˜ 등급은 " + customerGrade + 
                          "이며, λ³΄λ„ˆμŠ€ ν¬μΈνŠΈλŠ” " + bonusPoint + " μ μž…λ‹ˆλ‹€.";
              }
          }        
        
      • VIPCustomer.java

          public class VIPCustomer extends Customer{
                        
              double salesRatio;	//할인λ₯ 
        
              public VIPCustomer(String customerName) {
                            
                  super(customerName);
                            
                  customerGrade = "VIP";
                  bonusRatio = 0.05;
                  salesRatio = 0.1;
              }
                        
                        
              @Override
              //μƒν’ˆ 가격 할인 > calcPrice λ©”μ„œλ“œ μ˜€λ²„λΌμ΄λ”©-μž¬μ •μ˜ 
              public int calcPrice(int price) {
          //		return super.calcPrice(price);
          //		//μ›λž˜ μžˆλŠ” κ΅¬ν˜„ κ·ΈλŒ€λ‘œ λ°˜ν™˜
                            
                  bonusPoint += price * bonusRatio;
                  price -= (int)(price * salesRatio);
                  return price;
              }
                        
              public String showCustomerInfo() {
                  return customerName + "λ‹˜μ˜ 등급은 " + customerGrade + 
                          "이며, λ³΄λ„ˆμŠ€ ν¬μΈνŠΈλŠ” " + bonusPoint + " μ μž…λ‹ˆλ‹€.";
              }
          }
        
      • Main.java

          public class Main {
        
              public static void main(String[] args) {
        
                  Customer customerA = new Customer("λ°•ν•˜λŠ˜");
                  customerA.bonusPoint = 1000;
                  int price = customerA.calcPrice(30000);
                  System.out.println(customerA.showCustomerInfo());
                  System.out.println("μ§€λΆˆν•˜μ‹€ κΈˆμ•‘μ€ " + price + "원 μž…λ‹ˆλ‹€.");
                            
                  VIPCustomer customerB = new VIPCustomer("κΉ€λ°”λ‹€");
                  customerB.bonusPoint = 15000;
                  price = customerB.calcPrice(30000);
                  System.out.println(customerB.showCustomerInfo());
                  System.out.println("μ§€λΆˆν•˜μ‹€ κΈˆμ•‘μ€ " + price + "원 μž…λ‹ˆλ‹€.");
        
              }
          }
        
      • 좜λ ₯κ²°κ³Ό
        좜λ ₯κ²°κ³Ό

Categories:

Java