JAVA 공부하기 91.

this의 사용

 

public class ThisTest4

{

static String str1;

 

ThisTest4()

{ this("jsun"); }        // 생성자에서 생성자 호출

 

ThisTest4(String v1)

{ str1="hello"+ v1; }

 

public static void main(String[] args)

{

ThisTest4 obj1= new ThisTest4 ();

System.out.println(obj1.str1);

}

}

 

 


 

 

public class ThisTest5

{

int Q1=4;

 

public static void main(String[] args)

{

ThisTest5 obj1=new ThisTest5();

obj1.method1(6);

}

 

public void method1(int v1)

{

this.Q1 +=v1;

System.out.println(this.Q1);        // this.변수

}

}

 

 

 

 

 

JAVA공부하기 90

 

this의 사용

 

public class ThisTest2

{

int i=4;

 

public static void main(String args[])

{

ThisTest2 obj1 = new ThisTest2();

obj1.mth1();

System.out.println(this.i);

}

 

void mth1()

{

int i=10;

System.out.println("local i=" +i);

System.out.println("Member i=" + this.i);

}

}

 


 

public class ThisTest3

{

public static void main(String[] args)

{

ThisTest3 obj1 = new ThisTest3();

obj1.mth1();

}

public void mth1()

{

int result1= this.mth2(2,4);        // this.메서드

System.out.println(result1);

}

public int mth2(int v1, int v2)

{

int v3=v1+v2;

return v3;

}

}

 

 

 

JAVA 공부하기 89

 

this의 의미

- this는 자기 자신을 포함시키는 객체를 말함

 

 

 

this의 사용법

 

this 

언젠가 생성될 자신 객체의 메모리의 주소

 

this. 멤버필드

- 클래스내의 자신의 멤버를 직접 이용

- 메서드의 매개변수와 클래스의 멤버필드가 동일할 때 이를 구분하기 위해 사용

- this 키워드를 사용하지 않아도 무방

 

this. 멤버 메서드

- this. 멤버필드와 마찬가지로 자신의 멤버 메서드를 직접 이용

 

this()

- 클래스 자신의 생성자 메서드를 호출할 때 사용

- 자신의 생성자 메서드를 재이용

: 생성자 메서드에서 다른 생성자 메서드를 호출할 때 생성자 메서드의 호출은 제일 윗부분에서 사용해야함

 

+ Recent posts