자바_기초
생성자 - 오버로딩
잠수콩
2019. 5. 27. 08:11
// 회원가입 Join
// * 꼭 기입 이름,전화번호,주소
// 생년월일, 직장주소, 직장전화번호
class Join{
String name;
String phone;
String address;
String birth;
String work_address;
String work_phone;
//우클릭 > source > Generate Constructor Using Fields
public Join(String name, String phone, String address, String birth, String work_address, String work_phone) {
this.name = name;
this.phone = phone;
this.address = address;
this.birth = birth;
this.work_address = work_address;
this.work_phone = work_phone;
System.out.println("3");
}
// 꼭기입하는 입력값 생성자
Join(String name, String phone, String address) {
// 변수는 가장 가까운 범위
// 변수명이 동일 할 경우(클래스멤버랑)
// this.클래스멥버변수명
this.name = name;
this.phone = phone;
this.address = address;
System.out.println("4");
}
}
public class ConstructorEx {
public static void main(String[] args) {
// 생성자 오버로딩
new Join("홍길동","010-1234-1234","분당");
new Join("홍길동","010-2134-2134","서울","990101","서울","1234-1234");
// 생성자 ?
// - 인스턴스가 생성될때 자동적으로
// 호출 되는 인스턴스 초기화 메서드
// 클래스 생성자가 하나 이상은 있다.
// 생성자 조건
// 클래스명과 동일
// 생성자는 반환값 X , void 쓰지않아도 된다.
// 클래스명(변수명,변수명2,...){
// 인스턴스 생성시 수행될 코드
// 인스턴스 변수 초기화 코드!
}
}