728x90
반응형
InhEx05.java
class B1 { int x; } class B2 extends B1 { String x; } //동일 class 동급 public class InhEx05 { public static void main(String[] args) { B2 b2 = new B2(); //cha cha 자식것만 b2.x = "자바 상속 extends"; System.out.println("b2.x : " + b2.x); B1 b1 = new B1(); b1.x = 5000; System.out.println("b1.x : " + b1.x); } }
b2.x : 자바 상속 extends b1.x : 5000
InhEx06.java
//static pa cha
class C1 { static int x; //static 정적,공유되는,클래스명으로 접근 static int y; } //클래스 변수도 상속됨 class C2 extends C1 { static String x; } public class InhEx06 { public static void main(String[] args) { C2.x = "알기쉽게 해설한 자바"; //C1 클래스에서 선언된 int x는 가려짐 C2.y = 20000; C1.x = 30000; System.out.println("C2.x : " + C2.x); System.out.println("C2.y : " + C2.y); System.out.println("C1.x : " + C1.x); } }
C2.x : 알기쉽게 해설한 자바 C2.y : 20000 C1.x : 30000
public class InhEx07 { public static void main(String[] args) { Pt pt = new Pt(); pt.display(); //외부에서 접근 Cd ch = new Cd(); ch.display(); Pt ch1 = new Cd(); ch1.display(); } } class Pt { private int a = 10; //private로 Pt클래스 이내에서만. 외부 pt.으로 접근 불가 public int b = 20; //1.pa cha. 2.외부접근 확인 3.접근제어자에 따라 내부접근 정리 void display() { //pt.display();외부에서 접근 후 {}는 해당 클래스 내에서 실행 System.out.println("Pt_a: " + a); //10 System.out.println("Pt_b: " + b); //20 System.out.println("====================="); //"=====" } public int getA() { //외부접근 후 return내부접근 return a; } } class Cd extends Pt { public int c = 30; void display() { // System.out.printlin("Cd_a: " + a); 상속받은 private 필드 가려짐. System.out.println("Cd_a: " + getA()); //getA() 소속없을시 부모클래스도 호출가능 System.out.println("Cd_b: " + b); //public이라 접근가능 // 상속받은 public 필드 참조 System.out.println("Cd_c: " + c); //public이라 접근가능 // 자식 클래스에서 선언한 public 필드 참조 System.out.println("====================="); } }
Pt_a: 10 Pt_b: 20 ===================== Cd_a: 10 Cd_b: 20 Cd_c: 30 ===================== Cd_a: 10 Cd_b: 20 Cd_c: 30 =====================
Super01.java
//Cha Cha
class Pa3 { int x = 1000; void display() { System.out.println("상위클래스 display()"); } } class Ch3 extends Pa3 { int x = 2000; void display() { System.out.println("하위클래스 display()"); } void write() { super.display(); this.display(); System.out.println("super.x : " + super.x); System.out.println("this.x : " + x); } } public class Super01 { public static void main(String[] args) { Ch3 d = new Ch3(); d.write(); } }
상위클래스 display() 하위클래스 display() super.x : 1000 this.x : 2000
this 키워드 : 나의 인스턴스 this( ) 호출문 : 나의 인스턴스 안의 기본생성자 호출 this(매개변수...) 호출문 : 나의 인스턴스 안의 해당 케이스의 기타 생성자 호출 |
|
super 키워드 : 부모 인스턴스 super( ) : 부모 인스턴스의 기본생성자 호출(자동숨김) super(매개변수...) : 부모 인스턴스 안의 해당 케이스의 기타 생성자 호출 (반드시 기술해야한다) |
자식의 생성자 안에만 최초명령 |
Super02.java
super, this
class One { int d1; int s; One(){ System.out.println("One의 디폴트 생성자"); } One(int s1) { System.out.println("One의 생성자"); s = s1; d1 = s * s; } } class Two extends One { int d2; //400 int t; //20 Two() { this(10, 20); //Two(10,20) System.out.println("Two의 디폴트 생성자"); } Two(int s1, int t1) {//10, 20 super(s1); //주석처리 후 실행해 볼 것 //One(10); System.out.println("Two의 생성자"); t = t1; d2 = t * t; } } public class Super02 { public static void main(String[] args) { Two super1 = new Two(); Two super2 = new Two(10, 20); System.out.println("10의 제곱 : " + super2.d1); System.out.println("20의 제곱 : " + super2.d2); } }
One의 생성자 Two의 생성자 Two의 디폴트 생성자 One의 생성자 Two의 생성자 10의 제곱 : 100 20의 제곱 : 400
class One { int d1; //0 자동초기화값 int s; //0 자동초기화값 One(){ System.out.println("One의 디폴트 생성자"); } One(int s1) { System.out.println("One의 생성자"); s = s1; d1 = s * s; } } class Two extends One { int d2; //400 int t; //20 Two() { this(10, 20); //Two(10,20) System.out.println("Two의 디폴트 생성자"); } Two(int s1, int t1) {//10, 20 //super()기본생성자 - 자식의 기타생성자 안에 숨겨져있어서 실행해야함 System.out.println("Two의 생성자"); t = t1; d2 = t * t; } } public class Super02 { public static void main(String[] args) { Two super2 = new Two(); System.out.println("10의 제곱 : " + super2.d1); System.out.println("20의 제곱 : " + super2.d2); } }
One의 디폴트 생성자 Two의 생성자 Two의 디폴트 생성자 10의 제곱 : 0 20의 제곱 : 400
Super03.java
상속to상속
class AA1 { double d1; AA1() { System.out.println("AA1의 생성자"); d1 = 10 * 10; } } class AA2 extends AA1 { double d2; AA2() { super(); System.out.println("AA2의 생성자"); d2 = 10 * 10 * 10; } } class AA3 extends AA2 { double d3; AA3() { super(); System.out.println("AA3의 생성자"); d3 = 10 * 10 * 10 * 10; } } public class Super03 { public static void main(String[] args) { AA3 super1 = new AA3(); System.out.println("10의 2제곱 : " + super1.d1); System.out.println("10의 3제곱 : " + super1.d2); System.out.println("10의 4제곱 : " + super1.d3); } }
AA1의 생성자 AA2의 생성자 AA3의 생성자 10의 2제곱 : 100.0 10의 3제곱 : 1000.0 10의 4제곱 : 10000.0
CastingTest01.java
package inheritance; public class CastingTest01 { public static void main(String[] args) { //레퍼런스 student를 이용하면 name, dept에 접근 가능 Student student = new Student("Duly"); System.out.println(student.name); //Student 객체의 멤버 중 오직 Person 클래스의 멤버만 접근이 가능 Person person = student; person.name = "Gogildong"; System.out.println(person.name); //아래 문장은 컴파일 타임 오류 //person.dept = "Computer Eng"; //Pa-Cha라서 외부접근 불가 // ((Student)person).dept = "Computer Eng"; //외부 접근 필요시 일시적으로 다운캐스팅(다운할 클래스의 자료가 있어야 가능)후 호출하면 가능 //System.out.println(person.dept); } } class Person { String name; public Person(String name) { this.name = name; } } class Student extends Person { String dept; public Student(String name) { super(name); //생략해서 super()로 호출하더라도 Pa에서 기본생성자가 없기때문에 오류남 } }
Duly Gogildong
CastingTest02.java
package inheritance; public class CastingTest02 { public static void main(String[] args) { Guest guest = new Guest(); Food foodone = new Food(); guest.foodEat(foodone); foodone = new Jjigae(); guest.foodEat(foodone); foodone = new Tang(); guest.foodEat(foodone); } } class Food { public void cook() { System.out.println("Food 간맞추기"); } } class Jjigae extends Food { public void cook() { System.out.println("Jjigae 간맞추기"); } } class Tang extends Food { public void cook() { System.out.println("Tang 간맞추기"); } } class Guest { //public Guest(){} 숨겨져있음 public void foodEat(Food somefood) { System.out.println("Guest 간맞추기"); somefood.cook(); } }
Guest 간맞추기 Food 간맞추기 Guest 간맞추기 Jjigae 간맞추기 Guest 간맞추기 Tang 간맞추기
728x90
반응형
'[JAVA]' 카테고리의 다른 글
상속, 오버라이딩 (0) | 2023.12.27 |
---|---|
[12일차 오후] (0) | 2023.12.27 |
[11일차 오후] 상속 오버라이딩 (0) | 2023.12.26 |
[11일차 오전] 아스테리스크 (0) | 2023.12.26 |
[20231222] 10일차 오후 (0) | 2023.12.22 |