2013-04-07 2 views
33

나는이 코드 블록을 만났으며,이 한 줄은 내가 의미를 이해하지 못하거나 무엇을하고 있는지를 알지 못한다."this()"메서드는 무엇을 의미합니까?

public Digraph(In in) { 
    this(in.readInt()); 
    int E = in.readInt(); 
    for (int i = 0; i < E; i++) { 
     int v = in.readInt(); 
     int w = in.readInt(); 
     addEdge(v, w); 
    } 
} 

나는 this.method() 또는 this.variable이 무엇인지 이해하지만 this() 무엇인가?

+0

@Avi 난 그냥이 익숙한 소리 생각입니다. –

답변

48

이것은 생성자 오버로드가 있습니다. 이것은 확장 클래스를 초기화하기 위해 생성자의 첫 번째 줄에 super()을 호출하는 것과 매우 비슷합니다. 생성자의 첫 번째 줄에 this() (또는 다른 오버로드 인 this())을 호출해야 생성자 코드 중복을 피할 수 있습니다. Constructor overloading in Java - best practice

+0

감사합니다. 톤 ... 사실 니스 정보 – Gattsu

10

this()를 이와 같은 함수로 사용하면 본질적으로 클래스의 생성자를 호출합니다. 이를 통해 한 생성자의 모든 일반 초기화를 수행하고 다른 생성자를 전문화 할 수 있습니다. 예를 들어 코드 조각에서 this(in.readInt())을 호출하면 하나의 int 인수가있는 Digraph 생성자가 호출됩니다.

3

int 매개 변수를 사용하여 Digraph 클래스의 다른 생성자.

Digraph(int param) { /* */ } 
8

이 코드 단편은 생성자입니다.

this이 호출은 우리가 String 취하는 int 하나를 사용하는 생성자를 가지고 위의 예에서 같은 클래스

public App(int input) { 
} 

public App(String input) { 
    this(Integer.parseInt(input)); 
} 

의 또 다른 생성자를 호출합니다. String을 사용하는 생성자는 Stringint으로 변환 한 다음 int 생성자로 위임합니다.

다른 생성자 또는 수퍼 클래스 생성자 (super())에 대한 호출은 생성자의 첫 번째 줄이어야합니다.

생성자 오버로딩에 대한 자세한 설명은 this을 참조하십시오.

public class Diagraph { 

    public Diagraph(int n) { 
     // Constructor code 
    } 


    public Digraph(In in) { 
     this(in.readInt()); // Calls the constructor above. 
     int E = in.readInt(); 
     for (int i = 0; i < E; i++) { 
     int v = in.readInt(); 
     int w = in.readInt(); 
     addEdge(v, w); 
     } 
    } 
} 

당신이 코드를 말할 수는 생성자가 아닌 반환 형식의 부족에 의한 방법 :

3

this를 호출하면 기본적으로 클래스 생성자 호출

은 또한이 게시물을 볼 수있다.당신이 add(JComponent)과 함께보다, 뭔가를 확장하는 경우 예를 들어, 당신은 할 수 : this.add(JComponent).

2

생성자 오버로드를 :

예 :

public class Test{ 

    Test(){ 
     this(10); // calling constructor with one parameter 
     System.out.println("This is Default Constructor"); 
    } 

    Test(int number1){ 
     this(10,20); // calling constructor with two parameter 
     System.out.println("This is Parametrized Constructor with one argument "+number1); 
    } 

    Test(int number1,int number2){ 
     System.out.println("This is Parametrized Constructor with two argument"+number1+" , "+number2); 
    } 


    public static void main(String args[]){ 
     Test t = new Test(); 
     // first default constructor,then constructor with 1 parameter , then constructor with 2 parameters will be called 
    } 

} 
2

this();는 서로를 호출하는 데 사용됩니다 생성자 클래스의 생성자 예 : -

class A{ 
    public A(int,int) 
    { this(1.3,2.7);-->this will call default constructor 
    //code 
    } 
public A() 
    { 
    //code 
    } 
public A(float,float) 
    { this();-->this will call default type constructor 
    //code 
    } 
} 

참고 : 데드락 상태가 발생할 수 있으므로 기본 생성자에서 this() 생성자를 사용하지 않았습니다. 이 당신을 도울 것입니다

희망 :

4

그것은 거의 같은

public class Test { 
    public Test(int i) { /*construct*/ } 

    public Test(int i, String s){ this(i); /*construct*/ } 

} 
+0

귀하의 의견은 닫는 대괄호를 어지럽히고 있습니다. –

+0

@Alex 왜냐하면 나는 명확성을 위해 영업 담당자로부터 복사했기 때문입니다. – Antimony

관련 문제