day15_SuperTestEx21

육희영·2021년 10월 28일
0
package com.java1.day15;

//이전 예제와 비교해서 this.x 의 값이 10 아닌 20이 나왔다.
public class SuperTestEx21 {
	public static void main(String[] args) {
		Child2 c = new Child2();
		c.method();
	}
}

class Parent2 {
	int x = 10;
}

class Child2 extends Parent2 {
	int x = 20;	// 이전 예제에는 없었다..
	
	void method() {
		System.out.println("x=" + x); 
        System.out.println("this.x=" + this.x);	
        //이전 예제의 결과와 다르게 10 이 아닌 20을 출력한다.. this는 자기 자신을 의미 하기 때문이다. 
        System.out.println("super.x="+ super.x); 
	}
}

출력결과

x=20
this.x=20
super.x=10

0개의 댓글

관련 채용 정보