2009/03/06 18:18

1장 Visual Basic .net

 

Trackback 0 Comment 0
2008/12/27 16:56

16. 생성자

1. 생성자의 정의와 호출

 

(1) 새로운 객체에 대해서 기억 공간(힙 영역)이 할당

(2) 객체의 멤버변수(속성)를 초기화

(3) 생성자 호출

 

1.1 생성자 정의하기

 1. 생성자는 메서드의 일종이지만 메서드의 이름은 반드시 클래스 이름과 동일해야 한다.

2. 메서드의 앞에 리턴형을 기술하지 않는다. 함수의 자료형이 없다.

 

★ 생성자의 기본 형식

접근_지정자   클래스_이름(인수1, 인수2,...){

   문장1;

 

Ex>생성자 정의하기

001:class MyDate{  

002:  private int year;   

003:  private int month;   

004:  private int day;

005:  public  MyDate(){

006:    System.out.println("[생성자] : 객체가 생성될 때 자동 호출됩니다.");

007:  } 

008:  public void print(){

009:  System.out.println(year+ "/" +month+ "/" +day);

010:  }

011:

012:}

013:public class ConstructorTest02 {    

014:  public static void main(String[] args) {

015:    MyDate d =  new MyDate(); 

016:    d.print();

017:  }

018:}                        

 

Ex>생성자의 정의와 호출

001:class MyDate{  

002:  private int year;   

003:  private int month;   

004:  private int day;

005:  public MyDate(){

006:    year=2006;

007:    month=4;

008:    day=1;

009:  } 

010:  public void print(){

011:  System.out.println(year+ "/" +month+ "/" +day);

012:  }

013:}

014:public class ConstructorTest03 {    

015:  public static void main(String[] args) {

016:    MyDate d=new MyDate();

017:    d.print();

018:  }

019:}                                   

 

1.2 생성자 오버로딩

 

<예제> -전달인자를 갖는 생성자 정의

001:class MyDate{  

002:  private int year;   

003:  private int month;   

004:  private int day;

005:  //생성자는 속성(멤버변수)들의 초기화 작업을 목적으로 한다.

006:

007:  //[1] 전달인자 없는 생성자 정의

008:  public MyDate(){

009:    year=2006;    month=4;    day=1;

010:  } 

011:  //[2] 전달인자 있는 생성자 정의

012:  public MyDate(int new_year, int new_month, int new_day){

013:    year=new_year;

014:    month=new_month;

015:    day=new_day;

016:  }   

017:

018:  public void print(){

019:  System.out.println(year+ "/" +month+ "/" +day);

020:  }

021:}

022:

023:public class ConstructorTest04 {    

024:  public static void main(String[] args) {

025:    MyDate d=new MyDate(); 

026:    d.print();

027:

028:    MyDate d2=new MyDate(2007, 7, 19);

029:    d2.print();

030:  }

031:}     

 

<예제> 자바가 제공해주는 디폴트 생성자 호출

001:class MyDate{  

002:  private int year;   

003:  private int month;   

004:  private int day;

005:  public void print(){

006:     System.out.println(year+ "/" +month+ "/" +day);

007:  }

008:}

009:public class ConstructorTest01 {    

010:  public static void main(String[] args) {

011:    MyDate d=new MyDate();   //디폴트 생성자 호출

012:    d.print();

013:  }

014:}            

 

<예제>디폴트 생성자 부재로 인한 에러 발생

001:class MyDate{  

002:  private int year;   

003:  private int month;   

004:  private int day;

005:  public MyDate(int new_year, int new_month, int new_day){

006:    year=new_year;

007:    month=new_month;

008:    day=new_day;

009:  }   

010:  public void print(){

011:    System.out.println(year+ "/" +month+ "/" +day);

012:  }

013:}

014:

015:public class ConstructorTest05 {    

016:  public static void main(String[] args) {

017:    MyDate d=new MyDate(); 

018:    d.print();

019:

020:    MyDate d2=new MyDate(2007, 7, 19);

021:    d2.print();

022:  }

023:}                      

 

2. 레퍼런스 this

클래스를 구성하는 멤버변수는 객체 생성할 때마다 새롭게 메모리 할당을 하기 때문에 객체 단위로 따로 관리됩니다.

하지만 멤버함수는 모든 객체가 이 멤버함수를 공유해서 사용하기 때문에 어떤 객체에 의해서 사용되는 멤버함수인지 구분을 해야 할 목적으로 레퍼런스 this가 존재해야 합니다.

 

2.1 this를 사용해야만 하는 경우

메서드(생성자 포함)의 전달인자와 객체의 속성이 동일한 이름일 경우 전달인자와 속성이 구분되지 않기 때문에 문제가 발생하는데 이를 구분 짓기 위해서 속성 앞에 레퍼런스 this를 덧붙입니다.

 

<예제> 멤버변수와 속성이 동일해서 생긴 문제점

001:class MyDate{  

002:  private int year;   

003:  private int month;   

004:  private int day;

005:  //생성자 정의하기

006:  public MyDate(){

007:  }                

008:  public MyDate(int new_year, int new_month, int new_day){

009:    year=new_year;    month=new_month;    day=new_day;

010:  }

011:  //전달인자가 객체 속성의 이름과 동일한 메서드

012:  public void SetYear(int year){

013:    //this.year=year;

014:    year=year;

015:  }

016:  public void SetMonth(int new_month){

017:   month=new_month;

018: }

019:  public void print(){

020:  System.out.println(year+ "/" +month+ "/" +day);

021:  }

022:}

023:

024:public class ConstructorTest06 {    

025:  public static void main(String[] args) {

026:    MyDate d=new MyDate(2007, 7, 19);

027:    d.print();              

028:    d.SetYear(2008);   //변경되지 않음

029:    d.print();          //----------------Œ

030:    d.SetMonth(8);    //변경됨

031:    d.print();          //----------------

032:  }

033:}   

 

<예제> 속성(멤버변수) 앞에 레퍼런스 this 붙이기

001:class MyDate{  

002:  private int year;   

003:  private int month;   

004:  private int day;

005:  public MyDate(){

006:  }

007:  //생성자 역시 매개변수의 이름을 속성과 동일하게 줄 수 있다.

008:  public MyDate(int year, int month, int day){

009:    //멤버변수로 속성 값을 초기화하려면 대입연산자 왼쪽에 this를 붙여야 한다.

010:    this.year=year;  this.month=month;  this.day=day;

011:  }   

012:  public void SetYear(int year){ //대입연산자 왼쪽에 this를 붙였기에

013:    his.year=year;             //속성 값이 변경됨

014:  }

015:  public void SetMonth(int month){//대입연산자 왼쪽에 this를 붙였기에

016:    this.month=month;           //속성 값이 변경됨

017: }   

018: public void print(){

019:  System.out.println(year+ "/" +month+ "/" +day);

020:  }

021:}

022:

023:public class ConstructorTest07 {    

024:  public static void main(String[] args) {

025:    MyDate d=new MyDate(2007, 7, 19);

026:    d.print();

027:    d.SetYear(2008);  //2008년으로 변경

028:    d.SetMonth(8);    //8월로 변경

029:    d2.print();

030:  }

031:}             

 

3. 생성자 this( )

<예제> this로 생성자 호출하기

001:class MyDate{  

002:  private int year;   

003:  private int month;   

004:  private int day;

005:  public MyDate(){

006:    this(2006, 1, 1);                    //14:에 정의된 생성자 호출

007:  } 

008:  public MyDate(int new_year){

009:    this(new_year, 1, 1);                //14:에 정의된 생성자 호출

010:  } 

011:  public MyDate(int new_year, int new_month){

012:     this(new_year, new_month, 1);     //14:에 정의된 생성자 호출

013:  } 

014:  public MyDate(int new_year, int new_month, int new_day){

015:    year=new_year;

016:    month=new_month;

017:    day=new_day;

018:  }

019:

020:  public void print(){

021:  System.out.println(year+ "/" +month+ "/" +day);

022:  }

023:}

024:

025:public class ConstructorTest10 {    

026:  public static void main(String[] args) {

027:    MyDate d=new MyDate(2007, 7, 19);  //14:에 정의된 생성자 호출

028:    d.print();

029:    MyDate d2=new MyDate(2007, 7);     //11:에 정의된 생성자 호출

030:   d2.print();

031:    MyDate d3=new MyDate(2007);       //8:에 정의된 생성자 호출

032:    d3.print();

033:    MyDate d4=new MyDate();            //5:에 정의된 생성자 호출

034:    d4.print();

035:  }

036:}

 

Trackback 0 Comment 0
2008/12/27 16:50

VI 에디터 기본 명령어 및 단축키

VI 에디터에서의 자주 사용하는 기본적인 단축키 및 명령어

 ※ 대기모드

 

기능

기능

h

 왼쪽으로 이동

b

 한단어 뒤로 이동

j

 아래로 이동

B

 특수문자, 기호 제외

 한단어 뒤로 이동

k

 위로 이동

w

 한단어 앞으로 이동

l

 오른쪽으로 이동

W

 특수문자, 기호 제외

 한단어 앞으로 이동

o

 라인의 시작으로 이동

G

 파일의 마지막 행으로 이동

$

 라인의 끝으로 이동

+

 다음 라인의 처음으로 이동

^

 라인의 첫번째 단어로 이동

-

 이전 라인의 처음으로 이동

H

 화면의 맨위로 이동

[ctrl] + F

 한 화면 앞으로 이동

M

 화면의 중간으로 이동

[ctrl] + D

 한 화면의 반만큼 앞으로 이동

L

 화면의 끝으로 이동

[ctrl] + B

 한 화면의 뒤로 이동

/

 /[filename] 파일검색

[ctrl] + U

 한 화면의 반만큼 뒤로 이동

 

 입력/편집 모드

기능

기능

i

 현재 문자 앞에서부터 입력

dw

 한 단어 삭제

I

 현재 문장 앞에서부터 입력

dd

 한 라인 삭제

a

 현재 문자 뒤에서부터 입력

cw

 한 단어 바꾸기

A

 현재 문장 뒤에서부터 입력

x

 한 문자 삭제

o

 행의 아래에 입력

y

 문자 복사

O

 행의 위에 입력

p

 현재 줄 다음에

 버퍼의 내용을 붙임

r

 겹쳐쓰기

P

 현재 줄 앞에

 버퍼의 내용을 붙임

R

 현재문자 이후부터 겹쳐쓰기

u

 취소

 

 명령모드

명령

설명

 :O

 문서의 맨 앞으로 이동

 :$

 문서의 맨 뒤로 이동

 :set number

 행에 번호 부여

 :set nonumber

 행에 부여된 번호 제거

 :ZZ or :wq

 저장하고 vi 에디터 종료

 :w [filename]

 주어진 파일 이름으로 저장

 :w! [filename]

 주어진 파일 이름으로 저장/덮어쓰기

 :q

 저장하지 않고 vi 에디터 종료

 :q!

 저장하기 않고 vi 에디터 강제종료

 :e

 vi 에디터 종료하지 않고 다른 파일 편집

 :e!

 편집한 내용 저장하지 않고 최종 저장된 상태로 파일 열기


Trackback 0 Comment 0