[18] 생성자 함수
* 생성자(Constructor)함수
- new라는 명령어에 의해 객체가 생성될 때 자동으로 호출되며
자동으로 호출되는 생성자를 기본생성자 (default constructor)라 한다
- 기본생성자는 자바컴파일러에 의해 자동으로 생성된다
- 클래스 이름과 같아야 한다.(대소문자 구별)
- return Type이 없다.
- 오버로딩이 가능하므로 하나의 클래스에 여러개의 생성자가 있을 수 있다.
- new를 이용하여 객체를 메모리에 할당한 후 할당된 메모리에 특정 값으로
초기화하는 역할을 한다.
- 대부분 멤버변수의 초기값 할당 할 때 주로 사용한다
* 기본생성자를 반드시 선언해야 하는 경우
- 기본생성자는 기본적으로 자동생성 되지만
생성자 오버로딩이 필요할 경우에는
반드시 기본생성자를 수동적으로 만들어 줘야 한다
- 생성자에 초기화하는 변수가 없어도 반드시 생성자를 선언할 것을 권장한다
- 상속 관계에 들어가면 기본 생성자가 선언되지 않으면 상황에 따라 에러를
발생합니다.
- 기본 생성자는 하는 일이 없어도 상속시 에러 발생하는 경우가 있으니
수동적으로 무조건 선언을 적극 권장한다.
* 블럭단위 주석 : ctrl+shift+/
* eclipse에서 생성자 자동 생성
Source --> Generate Constructor using Fields...
파일명 : ConstructorEx1.java
package oop2;
class School
{
String name;
int kor;
int eng;
School()
{
}//기본생성자
School(int a)
{
kor=a;
}
School(int a, int b)
{
kor=a;
eng=b;
}
}
public class ConstructorEx1 {
public static void main(String[] args) {
//메소드(생성자함수) 가 호출된 상태
School kim=new School();
System.out.println(kim.kor);
System.out.println(kim.eng);
School lee=new School(80);
System.out.println(lee.kor);
School park=new School(50,40);
System.out.println(park.kor);
System.out.println(park.eng);
park.kor=50;
}
}
파일명 : ConstructorEx2.java
package oop2;
class Sungjuk
{
String name;
int kor;
int eng;
int mat;
int aver;
Sungjuk()
{
}//기본생성자
Sungjuk(String n,int k,int e,int m)
{
name=n;
kor=k;
eng=e;
mat=m;
}
}
//===============================
class Print
{
public Print()
{
}//기본생성자
void prn(Sungjuk k)
{
System.out.print(k.name+" ");
System.out.print(k.kor+" ");
System.out.print(k.eng+" ");
System.out.print(k.mat+" ");
System.out.println();
}
void prn(Mobile p)
{
System.out.print(p.company+" ");
System.out.print(p.model+" ");
System.out.print(p.price+" ");
System.out.println();
}
void prn(Calc c)
{
System.out.print(c.one+" ");
System.out.print(c.op+" ");
System.out.print(c.two+" ");
System.out.print("=");
System.out.print(c.one+c.two);
System.out.println();
}
}
//===========================
class Mobile
{
String company;
String model;
int price;
Mobile(){}
Mobile(String c,String m,int p)
{
company=c;
model=m;
price=p;
}
}
//===========================
class Calc
{
char op;
int one;
int two;
/* Calc(){}
Calc(char op)
{
this.op=op;
}
Calc(char op,int o)
{
op=op;
one=o;
}
Calc(char op,int o,int t)
{
this.op=op;
one=o;
two=t;
} */
public Calc() {
}
public Calc(char op) {
this.op = op;
}
public Calc(char op, int one) {
this.op = op;
this.one = one;
}
public Calc(char op, int one, int two) {
this.op = op;
this.one = one;
this.two = two;
}
}
//===========================
public class ConstructorEx2 {
public static void main(String[] args) {
/* Sungjuk kim=new Sungjuk();
kim.name="무궁화";
kim.kor=70;
kim.eng=80;
kim.mat=90;*/
Sungjuk kim=new Sungjuk("무궁화",70,80,90);
Sungjuk lee=new Sungjuk();
Print print=new Print();
print.prn(kim);
Mobile phone=new Mobile("SAMSUNG","Anycall",1000);
print.prn(phone);
Calc calc1=new Calc();
Calc calc2=new Calc('+');
Calc calc3=new Calc('+',10);
Calc calc4=new Calc('+',10,20);
print.prn(calc4); //10+20=30
}
}