728x90
반응형
오버로딩 |
|
- 리턴타입을 맞출 필요는 없다 | 부모 public void setA(){....} 자식 public int setA(int a){.........} |
- 상속관계에서도 오버로딩이 존재한다. | 부모 1) 2) public void md(int a){....} 자식클래스 extends 부모클래스 3) public void md( ){....} //오버라이드 4) public void md(String a){....} |
오버라이딩 |
|
- 반드시 재정의(덮어쓰기) + 리턴타입까지 동일해야 | 부모 void setA(int a){....} 자식 [default / protected / public] void setA(int a){.........} |
CastingTest03.java
package inheritance; class Person1 { private String name; public Person1(String name) { this.name = name; } public void setName(String name) { this.name = name; } public String getName() { return name; } } class Student1 extends Person1 { private String dept; //null public Student1(String name) { super(name); } public void setDept(String dept) { this.dept = dept; } public String getDept() { return dept; } } public class CastingTest03 { public static void main(String[] args) { //업캐스팅 선행 Person1 person1 = new Student1("Dully"); //Pa Cha //다운캐스팅, 명시적형변환, 강제변환 Student1 student1 = (Student1) person1; //여기에서만 다운캐스팅일어남 C-C // Student1 student1 = (Student1) new person1("MadPlay"); //person1자료값만 있어서 오류남 // student1.setName("Cogildong"); // person1.setName("Cogildong"); //person1 가능 String strName = student1.getName(); // String strName = person1.getName(); //person1 가능 // student1.setDept("Computer Eng"); // p-c에서 String strDept = student1.getDept(); //같은 주소값에서 cc값만 System.out.println(strName + ", " + strDept); } }
Dully, null
CastingTest04.javapackage inheritance; class PUnit { int u = 1;} class Unit1 extends PUnit { int u1 = 2; } class Unit2 extends Unit1 { int u2 = 3; } class Unit3 extends Unit2 { int u3 = 5; } public class CastingTest04 { public static void main(String[] args) { Unit2 unit1 = new Unit2(); // P, U1, U2 Unit3 unit2 = new Unit3(); // P, U1, U2, U3 int result = unit1.u + unit1.u1 + unit1.u2; // 1+2+3=6 int result1 = unit2.u + unit2.u1 + unit2.u2 + unit2.u3; //1+2+3+5=11 System.out.println( "1번: " + + unit1.u + " + " + unit1.u1 + " + " + unit1.u2 + " = " + result); System.out.println( "2번: " + + unit2.u + " + " + unit2.u1 + " + " + unit2.u2 + " + " + unit2.u3 + " = " + result); System.out.println("=============================="); System.out.println("0번: " + (unit1 instanceof PUnit)); System.out.println("1번: " + (unit1 instanceof Unit1)); System.out.println("2번: " + (unit1 instanceof Unit2)); System.out.println("3번: " + (unit2 instanceof PUnit)); System.out.println("4번: " + (unit2 instanceof Unit1)); System.out.println("5번: " + (unit2 instanceof Unit2)); System.out.println("6번: " + (unit2 instanceof Unit3)); } }
1번: 1 + 2 + 3 = 6 2번: 1 + 2 + 3 + 5 = 6 ============================== 0번: true 1번: true 2번: true 3번: true 4번: true 5번: true 6번: true
package inheritance; public class Customer { protected int customerID; protected String customerName; protected String customerGrade; int bonusPoint; double bonusRatio; public Customer() { customerGrade = "SILVER"; bonusRatio = 0.01; System.out.println("Cusomer() 생성자 호출"); } public Customer(int customerID, String customerName){ this.customerID = customerID; this.customerName = customerName; customerGrade = "SILVER"; bonusRatio = 0.01; System.out.println("Cusomer(int, String) 생성자 호출"); } public int calcPrice(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 inheritance; public class CustomerTest1 { public static void main(String[] args) { // Customer customerLee = new Customer(10010, "이순신"); Customer customerLee = new Customer(); customerLee.setCustomerID(10010); customerLee.setCustomerName("이순신"); customerLee.bonusPoint = 1000; System.out.println(customerLee.showCustomerInfo()); // VIPCustomer customerKim = new VIPCustomer(10020, "김유신", 12345); VIPCustomer customerKim = new VIPCustomer(); customerKim.setCustomerID(10020); customerKim.setCustomerName("김유신"); customerKim.bonusPoint = 10000; System.out.println(customerKim.showCustomerInfo()); } }
package inheritance; public class VIPCustomer extends Customer{ private int agentID; double saleRatio; public VIPCustomer() { //super() 숨겨져있음 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; } }
Cusomer() 생성자 호출 이순신 님의 등급은 SILVER이며, 보너스 포인트는 1000입니다. Cusomer() 생성자 호출 김유신 님의 등급은 VIP이며, 보너스 포인트는 10000입니다.
728x90
반응형
'[JAVA]' 카테고리의 다른 글
[13일차 오전] (0) | 2023.12.28 |
---|---|
상속, 오버라이딩 (0) | 2023.12.27 |
[12일차 오전] (0) | 2023.12.27 |
[11일차 오후] 상속 오버라이딩 (0) | 2023.12.26 |
[11일차 오전] 아스테리스크 (0) | 2023.12.26 |