하위 클래스를 생성하면 상위 클래스가 먼저 생성됨
package ch03;
public class Customer {
protected int customerID; //하위 클래스에선 접근 가능하지만 외부 클래스는 접근 불가
protected String customerName;
protected String customerGrade;
int bonusPoint; //package default ->다른 package에서 접근 불가
double bonusRatio;
/*public Customer() {
customerGrade="SILVER";
bonusRatio=0.01;
System.out.println("Customer() call");
}*/
public Customer(int customerID, String customerName) {
this.customerID=customerID;
this.customerName=customerName;
customerGrade="SILVER";
bonusRatio=0.01;
System.out.println("Customer(int,String) call");
}
public int clacPrice(int price) {
bonusPoint+=price*bonusRatio;
return price;
}
public String showCustomerInfo() {
return customerName+"님의 등급은 "+customerGrade+"이며 보너스포인트는 " + bonusPoint+"입니다.";
}
public int getCustomerID() {
return customerID;
}
public void setCustomerID(int customerID) {
this.customerID = customerID;
}
public String getCustomerName() {
return customerName;
}
public void setCustomerName(String customerName) {
this.customerName = customerName;
}
public String getCustomerGrade() {
return customerGrade;
}
public void setCustomerGrade(String customerGrade) {
this.customerGrade = customerGrade;
}
}
package ch03;
public class VIPCustomer extends Customer {
double salesRatio;
String agentID;
/*public VIPCustomer() {
// super(); 오류 발생
super(0,null); //상위 클래스의 생성자를 명시적으로 호출해줘야 O
bonusRatio=0.05;
salesRatio=0.1;
customerGrade="VIP";
System.out.println("VIP Customer() call");
}*/
public VIPCustomer(int customerID, String customerName) {
super(customerID,customerName);
//여기서 값 덮어씌우기!
customerGrade="VIP";
bonusRatio=0.1;
System.out.println("VIP Customer(int,String) call");
}
}
package ch03;
public class CustomerTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
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());
}
}
'WEB > Java' 카테고리의 다른 글
[JAVA] 상속(inheritance) (0) | 2023.01.23 |
---|---|
[JAVA] 객체배열 ArrayList (0) | 2023.01.23 |
[JAVA] 객체지향입문- 배열 (0) | 2023.01.22 |
[JAVA] 객체지향입문- static 변수 (0) | 2023.01.22 |
[JAVA] 객체지향입문- 객체 자신을 가리키는 this (0) | 2023.01.22 |