public class ConstructorDemo { => 생성자 없음 -> 자바가 자동으로 기본 생성자(클래스의 이름과 같고 매개변수 없는 메소드) 만들어줌
public static void main(String[] args) {
ConstructorDemo c = new ConstructorDemo();
}
}
=> ConstructorDemo 객체 생성할 때 자동으로 생성자를 만들어줘서 에러 X
public class ConstructorDemo {
public ConstructorDemo(int para1) {} => 생성자에 매개변수 있음-> 자바가 자동으로 기본 생성자를 만들어주지 않음
public static void main(String[] args) { ConstructorDemo c = new ConstructorDemo(); => 인자 없음 -> 에러 발생
}
public class ConstructorDemo {
public ConstructorDemo() {} => 인자가 없는 기본 생성자 명시적으로 선언
public ConstructorDemo(int para1) {}
public static void main(String[] args) {
ConstructorDemo c = new ConstructorDemo();
}
}
ㅠㅠㅠㅠ왜 계속 오류 뜨고 공백만 출력될까... type calculator is already defined...
super
- 기본생성자 X
class Calculator {
int left, right;
public Calculator(int left, int right) { => 인자 받음, 기본생성자 존재 X
this.left = left;
this.right = right;
}
public void setOprands(int left, int right) {
this.left = left;
this.right = right;
}
public void sum() {
System.out.println(this.left + this.right);
}
public void avg() {
System.out.println((this.left + this.right)/2);
}
}
class SubCalculator extends Calculator { public SubCalculator(int left, int right) { => 오류 발생 => Subcalculator 클래스를 인스턴스화시키면 Subcalculator의 생성자를 호출하기 전, 부모클래스 Calculator의 기본 생성자를 자동으로 호출하도록 약속돼있음 => but 기본생성자가 아닌 생성자를 명시적으로 정의해둬서 에러
this.left = left;
this.right = right;
}
public void substract() {
System.out.println(this.left - this.right);
}
public class CalculatorConstructorDemo4 {
public static void main(String[] args) {
SubCalculator c1 = new SubCalculator(10,20);
c1.sum();
c1.avg();
c1.substract();
}
}}
- 기본생성자 O
class Calculator {
int left, right;
public Calculator() {}
public Calculator(int left, int right) {
this.left = left;
this.right = right;
}
public void setOprands(int left, int right) {
this.left = left;
this.right = right;
}
public void sum() {
System.out.println(this.left + this.right);
}
public void avg() {
System.out.println((this.left + this.right)/2);
}
}
class SubCalculator extends Calculator {
public SubCalculator(int left, int right) {
this.left = left;
this.right = right;
}
public void substract() {
System.out.println(this.left - this.right);
}
public class CalculatorConstructorDemo4 {
public static void main(String[] args) {
SubCalculator c1 = new SubCalculator(10,20);
c1.sum();
c1.avg();
c1.substract();
}
}}
- super : 하위클래스에서 상위클래스 생성자 호출, 실행 -> 중복 문제 해결
= 부모클래스 (this와 같음)
class Calculator {
int left, right;
public Calculator() {}
public Calculator(int left, int right) {
this.left = left;
this.right = right;
}
public void setOprands(int left, int right) {
this.left = left;
this.right = right;
}
public void sum() {
System.out.println(this.left + this.right);
}
public void avg() {
System.out.println((this.left + this.right)/2);
}
}
class SubCalculator extends Calculator {
public SubCalculator(int left, int right) {
super(left, right); => 부모클래스의 생성자 / 초기화코드는 super 다음줄부터만 가능(상위클래스의 초기화가 다 끝난 상태에서만 하위클래스 초기화 진행)
}
public void substract() {
System.out.println(this.left - this.right);
}
public class CalculatorConstructorDemo4 {
public static void main(String[] args) {
SubCalculator c1 = new SubCalculator(10,20);
c1.sum();
c1.avg();
c1.substract();
}
'Programming > 자바' 카테고리의 다른 글
생활코딩 자바 - overloading (0) | 2022.04.02 |
---|---|
생활코딩 자바 - overriding ?? (0) | 2022.04.02 |
생활코딩 자바 - 상속 (0) | 2022.04.02 |
생활코딩 자바 - 생성자 (0) | 2022.04.02 |
생활코딩 자바 - 유효범위 (0) | 2022.04.01 |