Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- springframeworkruntime
- Junit5
- 스프링프레임워크
- Git
- gitbash
- GitHub
- .out
- MVC
- 부스트코스
- MVC모듈
- assuming That
- 파일삭제
- springboot
- assume That
- assume
- securityconfig
- assume True
- 컴퓨터과학
- Spring
- 원격저장소
- springsecurity
- .idea
- swaggerUrl
- c언어
- Swagger
- container
- CS50
- 팀과제
- springmvc
- DispatcherServlet
Archives
- Today
- Total
도담이 먹여 살려야하는 집사
This 본문
통상적으로 필드와 동일한 이름을 갖는 매개변수를 사용
->이 경우 필드와 매개 변수 이름이 동일하기 때문에 생성자 내부에서 해당 필드에 접근 할 수 없음
->동일한 이름의 매개변수가 사용 우선순위가 높기 때문에
1. 객체 자신을 가르키는 것 ! (앞으로 생성될 객체의 주소를 담을 곳이라고 가정)
2. this 객체 자신( 생성자 호출) >> 원칙 : 여러개의 생성자 호출
class Socar {
String color;
String geartype;
int door;
Socar(){ //기본설정
this.color = "red";
this.geartype = "auto";
this.door =2;
}
Socar(String color, String geartype, int door) {
this.color = color;
this.geartype = geartype;
this.door = door;
}
void print(){
System.out.println(this.color + "/" + this.geartype + "/" + this.door);
}
}
This() 다른생성자 호출
생성자 오버로딩이 많아질 경우 생성자 간의 중복된 코드 발생할 경우가 많음 -> 필드 초기화 내용은 한 생성자에만 집중적으로 작성하고 나머지 생성자는 초기화 내용을 가지고 있는 생성자를 호출하는 방법으로 개선 가능 (중복코드를 줄이기 위함)
생성자의 첫 줄에서만 허용
호출되는 생성자의 매개변수에 맞게 제공해야함
Zcar(){ //기본설정
// this.color = "red";
// this.geartype = "auto";
// this.door =2;
this("red","auto",2); // 나를 다시 호출하네
System.out.println("default constructor");
}
'JAVA' 카테고리의 다른 글
List Collection Class (추가예정) (0) | 2020.09.01 |
---|---|
final (0) | 2020.08.27 |
Generic Type (2) | 2020.08.26 |
Array_Basic (0) | 2020.08.23 |
상속(Inheritance)-1 (1) | 2020.08.19 |
Comments