본문 바로가기
Java 배우기

Using the this Keyword 키워드 this 사용하기

by 노화방지 Anti-aging Hairstyle 2016. 1. 5.
반응형


인스턴스 메소드나 생성자 안에서, this는 현재 객체(current object) (그 메소드나 생성자를 호출한 객체)에 대한 참조(reference) 입니다. 즉, this는 현재 객체 입니다. 

this를 사용하여 인스턴스 메소드나 생성자로부터, 또한 메소드나 생성자 안에서 현재 객체멤버를 참조(refer)할 수 있습니다.

Within an instance method or a constructor, this is a reference to the current object — the object whose method or constructor is being called. You can refer to any member of the current object from within an instance method or a constructor by using this.


this를 필드로 사용하기  Using this with a Field

키워드 this를 사용하는 가장 보편적 이유는, 메소드나 생성자 패러미터가 필드를 따라오기 때문입니다.

The most common reason for using the this keyword is because a field is shadowed by a method or constructor parameter.

예를 들어, 클래스 Point는 아래와 같이 작성되었습니다

For example, the Point class was written like this

public class Point { public int x = 0; public int y = 0; //생성자 constructor public Point(int a, int b) { x = a; y = b; } }

하지만 아래 처럼 작성될 수 있습니다: but it could have been written like this:

public class Point { public int x = 0; public int y = 0; //생성자 constructor public Point(int x, int y) { this.x = x; this.y = y; } }


생성자의 각 아규먼트는 객체의 필드 중 하나를 따라옵니다 - 생성자 안에서 x 생성자의 첫째 아규먼트의 로컬 카피입니다. 
생성자가 Point 필드 x를 참조하려면, 생성자는 this.x 를 사용해야 합니다.

Each argument to the constructor shadows one of the object's fields — inside the constructor x is a local copy of the constructor's first argument. To refer to the Point field x, the constructor must use this.x.


생성자에서 this 사용하기  Using this with a Constructor


생성자 내로부터, 생성자 내에서, 동일한 클래스 내의 다른 생성자를 호출하는데도 키워드 this를 사용할 수 있습니다.
그렇게 하는 것을
명시적 생성자 호출(explicit constructor invocation) 이라고 합니다.
Objects 섹션내의 클래스와 다르게 구현된, 다른 클래스 Rectangle이 있습니다.

From within a constructor, you can also use the this keyword to call another constructor in the same class. Doing so is called an explicit constructor invocation. Here's another Rectangle class, with a different implementation from the one in the Objects section.

public class Rectangle {
    private int x, y;
    private int width, height;
        
    public Rectangle() {
        this(0, 0, 1, 1);
    }
    public Rectangle(int width, int height) {
        this(0, 0, width, height);
    }
    public Rectangle(int x, int y, int width, int height) {
        this.x = x;
      this.y = y;
      this.width = width;
      this.height = height;
    }
    ...
}


위 클래스에는 1세트의 생성자(4개)가 포함되어 있습니다.
각 생성자는 rectangle의 멤버변수의 일부 또는 전부를 초기화 합니다.
아규먼트가 초기 값을 제공하지 않은 모든 멤버변수의 기본 값(default value)을 생성자들이 제공합니다.

예를 들어, argument가 없는 생성자는 coordinates 0,0 1x1 Rectangle을 생성시킵니다. 

argument가 2개인 생성자는, width 및 height 안으로 전달하면서, 하지만 항상 0,0 coordinates를 사용해서, 4개의 argument를 가진 생성자를 call 합니다.
앞과 같이, 컴파일러는 어떤 생성자를 call 해야 할지를 아규먼트의 갯수 및 타입에 근거해서 결정합니다.

다른 생성자의 invocation이 있다면 그것은 생성자 안에서 first line이 되어야 합니다.

This class contains a set of constructors. Each constructor initializes some or all of the rectangle's member variables. The constructors provide a default value for any member variable whose initial value is not provided by an argument. For example, the no-argument constructor creates a 1x1 Rectangle at coordinates 0,0. The two-argument constructor calls the four-argument constructor, passing in the width and height but always using the 0,0 coordinates. As before, the compiler determines which constructor to call, based on the number and the type of arguments.

If present, the invocation of another constructor must be the first line in the constructor.


반응형

댓글