(JAVA)상속2
이번에는 긴장 해야할지도 모르겠다. 이해가 안가도 천천히 읽다보면 이해가 갈수도 있다. 나는 오래걸렸다...
우리는 저번 포스트에서 기본적인 상속의 작동에 대해서 알아보았다. https://acid7937.tistory.com/17
하지만 시도해 본사람이 있을지는 모르겠지만 생성자를 만들어서 인자를 매개변수에 넣게 되면 무슨짓을 해도 저번처럼 하면 상속이 안될 것이다. 오늘은 매개변수가 있을때 어떻게 해야 하는지 알아보겠다.
혹시 생성자가 인자와 매개변수 넣는게 기억이 안나면 여길 참조하자 https://acid7937.tistory.com/16
class HCalculator {
int left, right;
/*public void enter (int left, int right) {
this.left = left;
this.right = right;
}*/
public HCalculator(){}
public HCalculator(int left, int right){
this.left = left;
this.right = right;
}
public void avg() {
System.out.println((this.left + this.right) / 2);
}
public void sum() {
System.out.println(this.left + this.right);
}
}
class subtractable extends HCalculator{
public subtractable(){}
public subtractable (int left, int right){
this.left = left;
this.right = right;
}
public void subtract(){
System.out.println(this.left - this.right);
}
}
class multipliable extends subtractable
{
public void sum(){
System.out.println("실행결과는!!! 뚜둥 = " + (this.left +this.right));
}
public multipliable(int left,int right){
this.left = left;
this.right = right;
}
public void multiply(){
System.out.println(this.left * this.right);
}
}
public class inherit {
public static void main(String[] args) {
HCalculator c1 = new HCalculator(20,40);
// c1.enter(20,40);
c1.sum();
c1.avg();
subtractable c2 = new subtractable(30,40);
//c2.enter(30,40);
c2.sum();
c2.avg();
c2.subtract();
multipliable c3 = new multipliable(80,40);
// c3.enter(20,40);
c3.sum();
c3.avg();
c3.subtract();
c3.multiply();
}
}
시작 하기 전에 이전 포스트와 뭐가 달라 졌는지 살펴보자
1.
/*public void enter (int left, int right) {
this.left = left;
this.right = right;
}*/
당연한 소리지만... 이번에는 인자를 받으니깐 필요없게 되어 지웠다.
2.
public HCalculator(){}
public subtractable(){}
이미 수동으로 생성자를 만들었는데 기본 생성자가 갑자기 또 보인다?
3.
public subtractable (int left, int right){
this.left = left;
this.right = right;
자식에게서 this가 보인다...
subtractable 클래스를 예시로 크게 어떻게 작동이 되는지 알아보면...
1)
subtractable c2 = new subtractable(30,40);
인스턴스가 만들어 지고 인자를 받았다.
2)
class subtractable extends HCalculator{
가장 먼저 자식인 subtractable이 부모인 HCalculator를 상속 받기로 한다. 그런데 이들은 둘다 생성자가 있다.
3)
public HCalculator(){}
public HCalculator(int left, int right){
this.left = left;
this.right = right;
}
앞서 이해하기 편하게 요약하면 자식이 부모에게 돈을 받기전에 부모를 한번 초기화 시켜주고 돈을 받아야한다...
받은게 있으니깐... extends 타고 올라간뒤 자식이 부모한테 가서 초기화(생성자가 초기화 하는거임) 하려고 보니깐 (부모 능력 쓰려면 초기화 한번 하고 써야됨) 자바가 응 기본 생성자 만들어서 초기화 하면 그만이야~ 하는데
부모도 수동으로 매개변수 받고있네? 충돌나잖아... 자바는 이미 수동으로 만들어버린 생성자가 있으면 지혼자 생성자를 만들지 못해게 되어버린다... 자바가 값을 어떻게 초기화 하지... 고민하다 사용자가 직접 입력해준 기본 생성자인 Public HCalculator(){} 이걸로 초기화를 시켜버림.
혹시 이해 안가면 https://acid7937.tistory.com/16 이거 보고 와야됨...
public multipliable(int left,int right){
this.left = left;
this.right = right;
this로 전역변수에 값을 넣었다. 그다음부터는 자주 설명한 거라 알 것이라 생각하고 설명 안 하겠다.
음... 따로 글 만들어서 설명하는것보다 과정과 해석을 보는게 도움이 된다고 생각했다.
------------------------------------------------
여기서 질문이 생길수가 있다.
1) 그럼 부모가 생성자를 안 만들면 굳이 Public HCalculator(){} 이걸 써줄 필요가 없는건가? = 그렇다.
2) 아니 바보임? this 중복 보기 싫네... super를 왜 안씀? = 잠깐 대기
class HCalculator {
int left, right;
//public HCalculator(){}
public HCalculator(int left, int right){
this.left = left;
this.right = right;
}
public void avg() {
System.out.println((this.left + this.right) / 2);
}
public void sum() {
System.out.println(this.left + this.right);
}
}
class subtractable extends HCalculator{
//public subtractable(){}
public subtractable (int left, int right){
super(left,right);
/* this.left = left;
this.right = right;*/
}
public void subtract(){
System.out.println(this.left - this.right);
}
}
class multipliable extends subtractable
{
public void sum(){
System.out.println("실행결과는!!! 뚜둥 = " + (this.left +this.right));
}
public multipliable(int left,int right){
super(left,right);
}
public void multiply(){
System.out.println(this.left * this.right);
}
}
public class inherit {
public static void main(String[] args) {
HCalculator c1 = new HCalculator(20,40);
c1.sum();
c1.avg();
subtractable c2 = new subtractable(30,40);
c2.sum();
c2.avg();
c2.subtract();
multipliable c3 = new multipliable(80,40);
c3.sum();
c3.avg();
c3.subtract();
c3.multiply();
}
}
이번에도 바뀐점을 먼저 알아보자.
1. this 지우고 super가 생겼다!
2.Public HCalculator(){}가 사라졌다!
이번에는 간략하게 가겠다.
public HCalculator(int left, int right){
this.left = left;
this.right = right;
super를 써주면 부모의 매개변수 받는 기능을(생성자) 자식이 가져오게 된다.(값을 가져온다는게 아님 값은 예제처럼 따로따로 줄수 있음)
또한 super()를 쓰지 않고 super. 을 쓴다면 부모의 변수를 가져올수 있게 된다.
부모가 int a =10 이 있다면 자식이 super.을 써주게 된다면 자식이 10을 가져오게 된다.
앞서 설명한 this는 자신을 가져오고 super는 부모를 가져오는 것이다. 예제를 보고 이해를 해보자.
class father{
int a =10;
}
class son extends father{
int a = 20;
public void number(){
System.out.printf("부모는 = %d , 자식은 = %d" , super.a, this.a);
}
}
public class susuper {
public static void main(String[] args) {
son c1 = new son();
c1.number();
}
}
출력 결과는 아래와 같다.
부모는 = 10 , 자식은 = 20