package inheritance;
public class OverridingTest1 {
public static void main(String[] args) {
int price = 10000; //지역변수는 접근제어자 붙일 수 없다. 기타제어자 final만 가능.
Customer customerLee = new Customer(10010,"이순신");
System.out.println(customerLee.getCustomerName() + " 님이 지불해야 하는 금액은 " + customerLee.calcPrice(price) + "원입니다.");
//System.- java.lang out.-static //메소드명 매개변수
VIPCustomer customerKim = new VIPCustomer(10020,"김유신", 12345);//김유신 님이 지불해야 하는 금액은
System.out.println(customerKim.getCustomerName() + " 님이 지불해야 하는 금액은 " + customerKim.calcPrice(price) + "원입니다.");
Customer vc = new VIPCustomer(10030, "나몰라", 2000);
System.out.println(vc.getCustomerName() + " 님이 지불해야 하는 금액은 " + vc.calcPrice(price) + "원입니다.");
}
}
package inheritance;
public class Customer {
protected int customerID; //protected 다른 패키지중 상속자식까지
protected String customerName; //"이순신"
protected String customerGrade; //SILVER
int bonusPoint; //0
double bonusRatio; //0.01 //0.05
public Customer()
{
customerGrade = "SILVER";
bonusRatio = 0.01;
System.out.println("Customer() 생성자 호출");
}
//생성자오버로드 - 동일클래스, 매개변수개수, 매개변수순서, 매개변수 자료형
public Customer(int customerID, String customerName){
this.customerID = customerID;
this.customerName = customerName;
customerGrade = "SILVER";
bonusRatio = 0.01;
System.out.println("Customer(int, String) 생성자 호출");
}
public int calcPrice(int price){ //10000
bonusPoint += price * bonusRatio;
//bonusPoint = bonusPoint + price * bonusRatio
return price; //10000
}
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 inheritance;
public class VIPCustomer extends Customer{
//extends-
private int agentID; //12345
double saleRatio; //(default)-동일 패키지내 접근가능
public VIPCustomer()
{ //super() - extends상태라 첫 생성자의 첫 번째 줄에 숨겨져있음
customerGrade = "VIP";
bonusRatio = 0.05;
saleRatio = 0.1;
}
public VIPCustomer(int customerID, String customerName, int agentID){
super(customerID, customerName); //super기타생성자 숨길 수 없다.
customerGrade = "VIP";
bonusRatio = 0.05;
saleRatio = 0.1;
this.agentID = agentID;
}
public int calcPrice(int price){
bonusPoint += price * bonusRatio;
return price - (int)(price * saleRatio);
}
public int getAgentID(){
return agentID;
}
}
Customer(int, String) 생성자 호출
이순신 님이 지불해야 하는 금액은10000원입니다.
Customer(int, String) 생성자 호출
김유신 님이 지불해야 하는 금액은9000원입니다.
Customer(int, String) 생성자 호출
나몰라 님이 지불해야 하는 금액은9000원입니다.
OverridingHiding.java
package inheritance;
public class OverridingHiding {
public static void main(String[] args) {
Car.printCompany(1);
Truck.printCompany(2);
// Car c = new Car();
// c.printCompany(1);
// Truck t = new Truck();
// t.printCompany(2);
Car car = new Car("소나타", "현대 자동차", 3000 , 3);
Truck truck = new Truck("Volvo", "General Auto",5000, 10, 4);
car.printInfo(5);
truck.printInfo(6);
System.out.println();
car.printCompany(7);
truck.printCompany(8);
Car c = (Car)truck;
c.printInfo(9);
System.out.println();
c.printInfo1(10);
}
}
class Car {
String carName;
String company;
int price;
int num;
Car(){}
Car(String carName, String company, int price, int num) {
this.carName = carName;
this.company = company;
this.price = price;
this.num = num;
System.out.println(num +"번 Car()생성자 호출");
}
static void printCompany(int num) {
System.out.println(num +"번(Car의 printCompany()메소드): 현대 자동차");
}
public void printInfo(int num) {
System.out.printf("\n"+ num +"번(Car의 printInfo()메소드): "
+ "차종:%s \t제조사:%s \t가격:%d \t", carName,company,price);
}
public void printInfo1(int num) {
System.out.printf("\n"+ num +"번(Car의 printInfo()메소드): "
+ "차종:%s \t제조사:%s \t가격:%d \t", carName,company,price);
}
}
class Truck extends Car {
int carName;
int maxLoad;
Truck(){}
Truck(String carName, String company, int price, int maxLoad, int num) {
super(carName, company, price, num);
this.maxLoad = maxLoad;
System.out.println(num +"번 Truck()생성자 호출");
}
public static void printCompany(int num) {
System.out.println(num + "번(Truck의 printCompany()메소드): "
+ "General Auto Truck Division");
}
public void printInfo(int num) {
super.printInfo(num);
System.out.printf("(Truck의 printCompany()메소드) 적재중량:%d", maxLoad);
}
public void printInfo1(int num) {
System.out.printf(num+ "번 : (Truck의 printCompany()메소드) 적재중량:%d", maxLoad);
}
}
1번(Car의 printCompany()메소드): 현대 자동차
2번(Truck의 printCompany()메소드): General Auto Truck Division
3번 Car()생성자 호출
4번 Car()생성자 호출
4번 Truck()생성자 호출
5번(Car의 printInfo()메소드): 차종:소나타 제조사:현대 자동차 가격:3000
6번(Car의 printInfo()메소드): 차종:Volvo 제조사:General Auto 가격:5000 (Truck의 printCompany()메소드) 적재중량:10
7번(Car의 printCompany()메소드): 현대 자동차
8번(Truck의 printCompany()메소드): General Auto Truck Division
9번(Car의 printInfo()메소드): 차종:Volvo 제조사:General Auto 가격:5000 (Truck의 printCompany()메소드) 적재중량:10
10번 : (Truck의 printCompany()메소드) 적재중량:10