개발/Java
☕️Java:: 생성자 오버로딩이란?
hyuunii
2023. 7. 10. 13:45
생성자는 예컨대 이런식임.
new로 인스턴스를 생성할 때, 인스턴스를 초기화하는 기능을 함.
public class AmisEducationalDemand {
String educational_demand_requirement_check;
String educational_demand_requirement_input;
String educational_demand_prefer_way_check;
String educational_demand_prefer_scale_check;
//이 아랫부분이 생성자
public AmisEducationalDemand(String educational_demand_requirement_check, String educational_demand_requirement_input, String educational_demand_prefer_way_check, String educational_demand_prefer_scale_check) {
this.educational_demand_requirement_check = educational_demand_requirement_check;
this.educational_demand_requirement_input = educational_demand_requirement_input;
this.educational_demand_prefer_way_check = educational_demand_prefer_way_check;
this.educational_demand_prefer_scale_check = educational_demand_prefer_scale_check;
}
}
근데 위의 경우, 4개의 매개변수를 모두 적어줘야 에러가 안남.
네 개를 다 받을 필요가 없어 갯수를 수정하려면 ⎡생성자 오버로딩⎦을 사용하면 됨.
public class AmisEducationalDemand {
String educational_demand_requirement_check;
String educational_demand_requirement_input;
String educational_demand_prefer_way_check;
String educational_demand_prefer_scale_check;
//매개 변수가 안들어오는 경우에 호출되는 생성자①
public AmisEducationalDemand() {
}
//매개 변수가 4개 들어올 경우에 호출되는 생성자②
public AmisEducationalDemand(String educational_demand_requirement_check, String educational_demand_requirement_input, String educational_demand_prefer_way_check, String educational_demand_prefer_scale_check) {
this.educational_demand_requirement_check = educational_demand_requirement_check;
this.educational_demand_requirement_input = educational_demand_requirement_input;
this.educational_demand_prefer_way_check = educational_demand_prefer_way_check;
this.educational_demand_prefer_scale_check = educational_demand_prefer_scale_check;
}
}
생성자①은 매개변수를 전혀 받지 않는 [기본 생성자]임.
생성자②는 매개변수를 4개 받는 생성자임.
위의 코드들로 인스턴스를 생성해보면
public class AmisEducationalDemand {
String educational_demand_requirement_check;
String educational_demand_requirement_input;
String educational_demand_prefer_way_check;
String educational_demand_prefer_scale_check;
//매개 변수가 안들어오는 경우에 호출되는 생성자①
public AmisEducationalDemand() {
}
//매개 변수가 4개 들어올 경우에 호출되는 생성자②
public AmisEducationalDemand(String educational_demand_requirement_check, String educational_demand_requirement_input, String educational_demand_prefer_way_check, String educational_demand_prefer_scale_check) {
this.educational_demand_requirement_check = educational_demand_requirement_check;
this.educational_demand_requirement_input = educational_demand_requirement_input;
this.educational_demand_prefer_way_check = educational_demand_prefer_way_check;
this.educational_demand_prefer_scale_check = educational_demand_prefer_scale_check;
}
}
public static void main(String[] args) {
AmisEducationalDemand amisEducationalDemand1 = new AmisEducationalDemand();
AmisEducationalDemand amisEducationalDemand2 = new AmisEducationalDemand("체크", "인풋", "체크", "체크");
System.out.println("--------amisEducationalDemand1------------");
System.out.println(amisEducationalDemand1.educational_demand_requirement_check);
System.out.println(amisEducationalDemand1.educational_demand_requirement_input);
System.out.println(amisEducationalDemand1.educational_demand_prefer_way_check);
System.out.println(amisEducationalDemand1.educational_demand_prefer_scale_check);
System.out.println("--------amisEducationalDemand2------------");
System.out.println(amisEducationalDemand2.educational_demand_requirement_check);
System.out.println(amisEducationalDemand2.educational_demand_requirement_input);
System.out.println(amisEducationalDemand2.educational_demand_prefer_way_check);
System.out.println(amisEducationalDemand2.educational_demand_prefer_scale_check);
}
--------amisEducationalDemand1------------
null
null
null
null
--------amisEducationalDemand2------------
체크
인풋
체크
체크
위의 결과가 나옴.
생성자①의 경우, 매개변수 없이 인스턴스를 생성하기 때문에 각 타입의 기본값을 자바 컴파일러가 임의대로 출력함.
생성자②의 경우, 입력받은 매개변수 4개를 출력해줌.
위의 코드를 IntelliJ 등 IDE(Integrated Development and Learning Environment) 없이 실행해볼 수 있음.
https://www.w3schools.com/java/tryjava.asp?filename=demo_helloworld
여기서 가능함.
대신 코드는 아래와 같이 입력해야됨.
(클래스 네이밍을 Main으로 하고, 인스턴스 생성할때도 Main type으로 만들어줌. 그 외에는 차이 없음.)
public class Main {
String educational_demand_requirement_check;
String educational_demand_requirement_input;
String educational_demand_prefer_way_check;
String educational_demand_prefer_scale_check;
//매개 변수가 안들어오는 경우에 호출되는 생성자①
public Main() {
}
//매개 변수가 4개 들어올 경우에 호출되는 생성자②
public Main(String educational_demand_requirement_check, String educational_demand_requirement_input, String educational_demand_prefer_way_check, String educational_demand_prefer_scale_check) {
this.educational_demand_requirement_check = educational_demand_requirement_check;
this.educational_demand_requirement_input = educational_demand_requirement_input;
this.educational_demand_prefer_way_check = educational_demand_prefer_way_check;
this.educational_demand_prefer_scale_check = educational_demand_prefer_scale_check;
}
public static void main(String[] args) {
Main amisEducationalDemand1 = new Main();
Main amisEducationalDemand2 = new Main("체크", "인풋", "체크", "체크");
System.out.println("--------amisEducationalDemand1------------");
System.out.println(amisEducationalDemand1.educational_demand_requirement_check);
System.out.println(amisEducationalDemand1.educational_demand_requirement_input);
System.out.println(amisEducationalDemand1.educational_demand_prefer_way_check);
System.out.println(amisEducationalDemand1.educational_demand_prefer_scale_check);
System.out.println("--------amisEducationalDemand2------------");
System.out.println(amisEducationalDemand2.educational_demand_requirement_check);
System.out.println(amisEducationalDemand2.educational_demand_requirement_input);
System.out.println(amisEducationalDemand2.educational_demand_prefer_way_check);
System.out.println(amisEducationalDemand2.educational_demand_prefer_scale_check);
}
}