Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 |
Tags
- java #객체지향 #자바
- HashMap
- 메서드
- java
- Arrays.toString()
- 해쉬맵
- 자바 #JAVA
- 믹스인
- 이펙티브자바
- overring
- 객체지향프로그램
- java #continue #반복문
- java #자바 #상속 #오버라이딩
- 람다
- 자바 #변수 #java
- 인텔리제이
- java #조건문 #if문
- Math.sqrt()
- IntelliJ
- java # for문 #반복문
- 스프링 #Spring #spring #입문서
- java #중첩반복문 #구구단
- Mixin
- Math.pow()
- 자바 #java #상속
- java #조건문 #if조건문 #if
- java #자료형
- java #자바 #상속
- 자바
- EffectiveJava
Archives
- Today
- Total
산으로 가자▲
메서드 재정의하기(overring) 본문
메서드 재정의하기(overring)
하위 클래스에서 메서드 재정의 하기
- 오버라이딩(overriding) : 상위 클래스에 정의된 메서드의 구현 내용이 하위 클래스에서 구현할 내용과 맞지 않는 경우 하위 클래스에서 동일한 이름의 메서드를 재정의 할 수 있음
- VIPCustomer 클래스의 calcPrice()는 할인율이 적용되지 않음
- 재정의 하여 구현해야 함
VIPCustomer.java
@Override public int calcPrice(int price) { bonusPoint += price * bonusRatio; return price - (int)(price * salesRatio); }
@overriding 애노테이션 (annotation)
- 애노테이션은 원래 주석이라는 의미
- 컴파일러에게 특별한 정보를 제공해주는 역할
- @overriding 애노테이션은 재정의 된 메서드라는 의미로 선언부가 기존의 메서드와 다른 경우 에러가 남
형 변환과 오버라이딩 메서드 호출
Customer vc = new VIPCustomer();
vc 변수의 타입은 Customer지만 인스턴스의 타입은 VIPCustomer 임
자바에서는 항상 인스턴스의 메서드가 호출 됨 (가상메서드의 원리)
자바의 모든 메서드는 가상 메서드(virtual method) 임
CustomerTest.java
`public class CustomerTest {
public static void main(String[] args) {
Customer customerLee = new Customer(10010, "이순신");
customerLee.bonusPoint = 1000;
System.out.println(customerLee.showCustomerInfo());
VIPCustomer customerKim = new VIPCustomer(10020, "김유신");
customerKim.bonusPoint = 10000;
System.out.println(customerKim.showCustomerInfo());
int priceLee = customerLee.calcPrice(10000);
int priceKim = customerKim.calcPrice(10000);
System.out.println(customerLee.showCustomerInfo() + " 지불금액은 " + priceLee + "원 입니다.");
System.out.println(customerKim.showCustomerInfo() + " 지불금액은 " + priceKim + "원 입니다.");
Customer customerNo = new VIPCustomer(10030, "나몰라");
customerNo.bonusPoint = 10000;
int priceNo = customerNo.calcPrice(10000);
System.out.println(customerNo.showCustomerInfo() + " 지불금액은 " + priceNo + "원 입니다.");
}
}`
'Java' 카테고리의 다른 글
Math.Sqrt()란? (0) | 2022.10.05 |
---|---|
Java - 거듭제곱 계산, Math.pow() (0) | 2022.10.05 |
상속에서 클래스 생성 과정과 형 변환 (0) | 2022.10.05 |
상속을 활용한 멤버십 클래스 구현하기 (0) | 2022.10.04 |
객체 간의 상속은 어떤 의미일까? (0) | 2022.10.04 |