멤버변수의 값들을 초기화 하기 위해서 사용함~
생성자를 1개라도 코더가 제작하게 되면 더이상 기본 생성자는 제공되지 않습니다.
A constructor is typically used to initialize instance variables representing the main properties of the created object
In Java, a constructor is used to initialize an instance of a class. It is responsible for initializing the member variables of that instance. When you create a new object using the new keyword, the constructor is invoked to set up the initial state of the object.
The constructor is a special method within a class that has the same name as the class itself. It is typically defined without a return type. By providing one or more constructors in a class, you can control how objects of that class are initialized.
Inside the constructor, you can assign initial values to the member variables of the object being created. This allows you to set up the initial state of the object according to your requirements.
For example, consider a class Person with member variables like name and age. The constructor of the Person class can be used to initialize these member variables when a new Person object is created. Here's an example constructor:
public class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
// Other methods and code...
}
In the above example, the constructor Person(String name, int age) initializes the name and age member variables of the Person object based on the provided arguments.
package class04;
class CarB{
String name;
int speed;
int max;
// this() == 생성자 함수
CarB(){
this("무명",120);
System.out.println("확인 1");
}
CarB(String name){
this(name,120);
System.out.println("확인 2");
}
CarB(String name,int max){
this.name=name;
this.speed=0;
this.max=max;
System.out.println("확인 3");
}
void printInfo() {
System.out.println(this.name+"님의 차는 ["+this.speed+"/"+this.max+"]입니다.");
}
void speedUp() {
this.speed+=100;
if(this.speed>this.max) {
this.speed=this.max;
System.out.println("과속!");
}
}
void speedUp(int speed) { // 오버로딩
this.speed+=speed;
if(this.speed>this.max) {
this.speed=this.max;
System.out.println("과속!");
}
}
void speedDown() {
this.speed-=5;
if(this.speed<=0) {
this.speed=0;
System.out.println("정지...");
}
}
}
public class Test05CarAnswer02 {
public static void main(String[] args) {
CarB c1=new CarB(); // 53->9->10 => 17~22 =>11
CarB c2=new CarB("홍길동"); //54->13->14=>17~22->15
CarB c3=new CarB("아무무",200);
c1.printInfo();
c2.speedDown();
c2.printInfo();
c3.speedUp();
c3.speedUp(50);
c3.printInfo();
c3.speedUp(70);
c3.printInfo();
}
}
각기 다른(인수) 가 다른 객체를 사용하려 할때 '생성자 오버로딩'을 하게 됨
함수끼리는 영역을 공유하지 않기 때문에 선언된 멤버변수를 각각 생성자들 마다 초기화를 시켜줘야 하는 상황이 발생할수 있음
반복적인 cnt c + v를 줄이기 위해 this()[생성자함수]를 사용함
인스턴스화 할때 relevant한 생성자를 찾아감
- CarB c1=new CarB(); ===> CarB(){
생성자 함수를 만나면 생성자 함수에 포함된 input을 기준으로 다른 relevant 한 생성자를 찾아감
- this("무명",120); ===> CarB(String name,int max){
그리고 다시 기존의 인스턴스화 할대 찾았던 생성자에게 되돌아가서 다시 수행
- CarB(String name,int max) { ... System.out.println("확인 3");} ===> CarB() { ... System.out.println("확인1");}
package class04;
class A{
int a;
int b;
int c;
int d;
int e;
int f;
int g;
int h;
A(int a, int b, int c, int e, int f, int g, int h) {
this.a = a;
this.b = b;
this.c = c;
this.d = 0;
this.e = e;
this.f = f;
this.g = g;
this.h = h;
}
//인자값과 멤버변수 이름을 똑같이 맞춰줘요
// 그럼 인자변수 멤버변수 구분이 안감 ==>> this (나자신이야) 라고 알려주
// this == 자기자신객체
}