본문 바로가기
Backend/JAVA

[JAVA] constructor

by howdyoon 2023. 3. 15.



생성자 함수(Constructor)

- 클래스명과 동일한 함수
- new연산자와 함께 메모리 할당할 때 사용한다
- 오버로드가 가능하다

소멸자 함수(Destructor) -> 자바에는 없음

가비지 콜렉팅(Garbage Collecting)

- JVM이 자동으로 메모리 회수를 함.

//현재 년도 윤년, 평년 구하기
GregorianCalendar today=new GregorianCalendar();
		
if(today.isLeapYear(2023)) {
    System.out.println("윤년");
}else {
    System.out.println("평년");
}//if end

 

문) 서기1년~서기2023년까지 윤년의 갯수를 구하시오

//1)GregorianCalendar클래스 활용(추천)
int count1=0;
for(int y=1; y<=2023; y++) {
    if(today.isLeapYear(y)) {
        count1++;
    }//if end
}//for end
System.out.println(count1);//502

//2)윤년 구하는 공식
int count2=0;
for(int y=1; y<=2023; y++) {
    if(y%4==0 && y%100!=0 || y%400==0) {
        count2++;				
    }//if end
}//for end
System.out.println(count2);//490

 

- School클래스 생성후 실습

1)

package oop0315;

class School {
	//멤버변수 field
        private String name;
        private int kor, eng, mat;
        private int aver;
	
	//생성자함수 Constructor
	//->클래스명과 동일한 함수
	//->리턴형이 없다
	//->public void School(){} 쓰지 않도록 주의
	public School() {//기본 생성자 함수
                         //default constructor
                         //자동으로 생성된다
           System.out.println("School() 호출됨...");
	}//end 
	
	//멤버함수 method
	public void calc() {
		aver=(kor+eng+mat)/3;
	}//calc() end
	
	public void disp() {
          System.out.println(name);
          System.out.println(kor);
          System.out.println(eng);
          System.out.println(mat);
          System.out.println(aver);
	}//disp() end
}//class end

2)

School one=new School(); //#100번지
System.out.println(one.hashCode());
one.calc();
one.disp();

String a=new String(); //빈문자열
String b=null;		   //메모리 할당은 하지 않고 b라는 참조변수 선언만 해 놓음
System.out.println(a.length());//글자갯수 0
//System.out.println(b.length());//NullPointerException 발생

System.out.println("null"); //문자열 상수

3)

School one=new School("개나리");
one.calc();
one.disp();

School two=new School(70, 80, 90);
two.calc();
two.disp();

School three=new School("진달래", 10, 20, 30);
three.calc();
three.disp();

- School 클래스

package oop0316;

class School {

	//멤버변수 field column property attribute
	private String name;		
	private int kor,eng,mat;
	private int aver;
	
	
	//생성자 함수 constructor
	public School() {//기본 생성자 함수
		   			 //default constructor
					 //자동으로 생성된다
		System.out.println("School() 호출됨");
	}
	//생성자 함수도 오버로드(함수명 중복 정의)가 가능하다
	public School(String n) {
		name=n;
	}//end
	
	public School(int k, int e, int m) {
		kor=k;
		eng=e;
		mat=m;
	}//end
	
	public School(String n, int k, int e, int m) {
		name=n;
		kor=k;
		eng=e;
		mat=m;
	}//end
	
	//멤버함수 method
	void calc() {
		aver=(kor+eng+mat)/3;
	}//calc() end
	
	public void disp() {
		System.out.println(name);
		System.out.println(kor);
		System.out.println(eng);
		System.out.println(mat);
		System.out.println(aver);
	}//disp() end
	
}//class end

'Backend > JAVA' 카테고리의 다른 글

[JAVA] this와 this()  (0) 2023.03.16
[JAVA] String  (0) 2023.03.16
[JAVA] class  (0) 2023.03.15
[JAVA] main  (0) 2023.03.15
[JAVA] quiz: 배열-편차구하기  (0) 2023.03.15