티스토리에 저장했던 글을 옮겼습니다.
https://mrcocoball.tistory.com/79
public class Customer {
// private int customerID;
// private String customerName;
// private String customerGrade;
protected int customerID; // 상속된 자식 클래스에서도 접근할 수 있게 protected
protected String customerName; // 상속된 자식 클래스에서도 접근할 수 있게 protected
protected String customerGrade; // 상속된 자식 클래스에서도 접근할 수 있게 protected
int bonusPoint;
double bonusRatio;
// constructor
public Customer() {
customerGrade = "SILVER";
bonusRatio = 0.01;
}
// VIPCustomer에 대한 필드와 메소드를 넣기에는 부적절 (if, else문도 계속 많아질 것) -> 상속!!
// getter and setter
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;
}
// method
public int calcPrice(int price) {
bonusPoint += price * bonusRatio;
return price;
}
public String showCustomerInfo() {
return customerName + "님의 등급은 " + customerGrade + "이며, 보너스 포인트는 " + bonusPoint + "입니다.";
}
}
public class VIPCustomer extends Customer { // Customer 상속 받음
private int agentID;
double salesRatio;
// constructor
public VIPCustomer() {
bonusRatio = 0.05;
salesRatio = 0.1;
// customerGrade = "VIP"; Customer의 customerGrade가 private이기 때문에 접근 불가
customerGrade = "VIP";
}
// getter and setter
public int getAgentID() {
return agentID;
}
public void setAgentID(int agentID) {
this.agentID = agentID;
}
}
public class CustomerTest {
public static void main(String[] args) {
Customer customerRico = new Customer();
customerRico.setCustomerName("리코");
customerRico.setCustomerID(10010);
customerRico.bonusPoint = 1000;
System.out.println(customerRico.showCustomerInfo());
VIPCustomer customerMari = new VIPCustomer();
customerMari.setCustomerName("마리");
customerMari.setCustomerID(10011);
customerMari.bonusPoint = 10000;
System.out.println(customerMari.showCustomerInfo());
}
}
// constructor
/* public VIPCustomer() { //Customer() 기본 생성자가 없으므로 오류 발생
bonusRatio = 0.05;
salesRatio = 0.1;
// customerGrade = "VIP"; Customer의 customerGrade가 private이기 때문에 접근 불가
customerGrade = "VIP";
System.out.println("VIPCustomer() 생성자 호출");
} */
public VIPCustomer() {
super(0, "no-name");
bonusRatio = 0.05;
salesRatio = 0.1;
// customerGrade = "VIP"; Customer의 customerGrade가 private이기 때문에 접근 불가
customerGrade = "VIP";
}
public VIPCustomer(int customerID, String customerName) {
super(customerID, customerName);
bonusRatio = 0.05;
salesRatio = 0.1;
// customerGrade = "VIP"; Customer의 customerGrade가 private이기 때문에 접근 불가
customerGrade = "VIP";
}
부모타입 변수 = new 자식타입 생성자
Customer customerMari = new VIPCustomer();
VIPCustomer vCustomer = new VIPCustomer();
addCustomer(vCustomer);
int addCustomer(Customer customer){
}
Customer vc = new VIPCustomer(12345, "noname"); // 형변환 진행 vc의 타입은 Customer이지만 인스턴스는 VIPCustomer()임
price = vc.calcPrice(1000); // 재정의된 calcPrice 적용 (인스턴스의 메소드)
System.out.println(price);
@Override // Customer의 calcPrice 재정의(오버라이딩)
// source -> override로 활성화 가능
public int calcPrice(int price) {
bonusPoint += price * bonusRatio;
return price - (int)(price * salesRatio); // 해당 부분 재정의
}
public class CustomerTest {
public static void main(String[] args) {
Customer customerRico = new Customer(10010, "리코");
customerRico.bonusPoint = 1000;
int price = customerRico.calcPrice(1000);
System.out.println(customerRico.showCustomerInfo() + price);
VIPCustomer customerMari = new VIPCustomer(10011, "마리");
customerMari.bonusPoint = 10000;
price = customerMari.calcPrice(1000);
System.out.println(customerMari.showCustomerInfo() + price);
}
}
Customer vc = new VIPCustomer(12345, "noname"); // 형변환 진행 vc 변수의 타입은 Customer이지만 인스턴스는 VIPCustomer()임
price = vc.calcPrice(1000); // 재정의된 calcPrice 적용 (인스턴스의 메소드)
System.out.println(price);
class Idol {
public void call() {
System.out.println("아이돌이 콜 앤드 리스폰스합니다.");
}
public void dancing() {
}
}
class Rico extends Idol {
@Override
public void call() { // 재정의
System.out.println("리코가 콜 앤드 리스폰스합니다.");
}
public void raser() {
System.out.println("리코가 레이저 빔을 쏩니다.");
}
}
class Mari extends Idol {
@Override
public void call() { // 재정의
System.out.println("마리가 콜 앤드 리스폰스합니다.");
}
public void money() {
System.out.println("마리가 돈을 씁니다.");
}
}
class Yoshiko extends Idol {
@Override
public void call() { // 재정의
System.out.println("요시코가 콜 앤드 리스폰스합니다.");
}
public void fallen() {
System.out.println("요시코가 타천합니다.");
}
}
public class IdolTest {
public static void main(String[] args) {
Idol rIdol = new Rico(); // 형변환
Idol mIdol = new Mari(); // 형변환
Idol yIdol = new Yoshiko(); // 형변환
IdolTest test = new IdolTest();
test.callIdol(rIdol);
test.callIdol(mIdol);
test.callIdol(yIdol);
ArrayList<Idol> idolList = new ArrayList<>();
idolList.add(rIdol);
idolList.add(mIdol);
idolList.add(yIdol);
for (Idol idol : idolList) {
idol.call();
}
}
public void callIdol(Idol idol) { // Idol 타입의 하위 클래스 인스턴스에 따라 재정의한 call() 메소드 사용
idol.call();
// idol.fallen(); 안됨 (상위 클래스에 없으며 재정의되지 않았음) >> 사용하려면 다운 캐스팅 필요
// idol.money(); 안됨 (상위 클래스에 없으며 재정의되지 않았음) >> 사용하려면 다운 캐스팅 필요
// idol.raser(); 안됨 (상위 클래스에 없으며 재정의되지 않았음) >> 사용하려면 다운 캐스팅 필요
}
}
public class Customer {
// private int customerID;
// private String customerName;
// private String customerGrade;
protected int customerID; // 상속된 자식 클래스에서도 접근할 수 있게 protected
protected String customerName; // 상속된 자식 클래스에서도 접근할 수 있게 protected
protected String customerGrade; // 상속된 자식 클래스에서도 접근할 수 있게 protected
int bonusPoint;
double bonusRatio;
// constructor
public Customer(int customerID, String customerName) {
this.customerID = customerID;
this.customerName = customerName;
customerGrade = "SILVER";
bonusRatio = 0.01;
}
// VIPCustomer에 대한 필드와 메소드를 넣기에는 부적절 (if, else문도 계속 많아질 것) -> 상속!!
// getter and setter
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;
}
// method
public int calcPrice(int price) {
bonusPoint += price * bonusRatio;
return price;
}
public String showCustomerInfo() {
return customerName + "님의 등급은 " + customerGrade + "이며, 보너스 포인트는 " + bonusPoint + "입니다.";
}
}
public class GoldCustomer extends Customer {
double salesRatio;
// constructor
public GoldCustomer(int customerID, String customerName) {
super(customerID, customerName);
bonusRatio = 0.02;
salesRatio = 0.1;
// customerGrade = "VIP"; Customer의 customerGrade가 private이기 때문에 접근 불가
customerGrade = "GOLD";
}
@Override // Customer의 calcPrice 재정의(오버라이딩)
// source -> override로 활성화 가능
public int calcPrice(int price) {
bonusPoint += price * bonusRatio;
return price - (int)(price * salesRatio); // 해당 부분 재정의
}
}
public class VIPCustomer extends Customer { // Customer 상속 받음
private int agentID;
double salesRatio;
// constructor
public VIPCustomer(int customerID, String customerName, int agentID) {
super(customerID, customerName);
bonusRatio = 0.05;
salesRatio = 0.1;
this.agentID = agentID;
// customerGrade = "VIP"; Customer의 customerGrade가 private이기 때문에 접근 불가
customerGrade = "VIP";
}
// getter and setter
public int getAgentID() {
return agentID;
}
public void setAgentID(int agentID) {
this.agentID = agentID;
}
@Override // Customer의 calcPrice 재정의(오버라이딩)
// source -> override로 활성화 가능
public int calcPrice(int price) {
bonusPoint += price * bonusRatio;
return price - (int)(price * salesRatio); // 해당 부분 재정의
}
@Override
public String showCustomerInfo() {
return super.showCustomerInfo() + " 담당 상담원 번호는 " + agentID + " 입니다."; // super로 상위 클래스 메소드에 접근
}
}
public class CustomerTest {
public static void main(String[] args) {
ArrayList<Customer> customerList = new ArrayList<>();
Customer customerH = new Customer(10010, "하나마루");
Customer customerY = new Customer(10011, "요시코");
Customer customerR = new GoldCustomer(10012, "리코");
Customer customerD = new GoldCustomer(10013, "다이아");
Customer customerM = new VIPCustomer(10014, "마리", 12345);
customerList.add(customerH);
customerList.add(customerY);
customerList.add(customerR);
customerList.add(customerD);
customerList.add(customerM);
System.out.println("------------ 고객 정보 ------------");
for (Customer customer : customerList) {
System.out.println(customer.showCustomerInfo());
}
int price = 10000;
System.out.println("------------ 구매 정보 ------------");
for (Customer customer : customerList) {
int cost = customer.calcPrice(price);
System.out.println(customer.getCustomerName() + "님이 " + cost + "원 지불하셨습니다.");
System.out.println(customer.getCustomerName() + "님의 현재 보너스 포인트는 " + customer.bonusPoint + "입니다.");
}
}
}