클래스 이름에 "this"사용
나는 안드로이드 프로그래밍을하고 있고 C #을 훈련받은 마음에 펑키 한 것처럼 보이는 생성자를 보았을 때 인 텐트에 대해 배우고있었습니다. 전화는 :
Intent myIntent = new Intent(CurrentActivity.this, NextActivity.class);
두 매개 변수 모두 나에게 새로운 것입니다. 클래스 이름에서 정적 ".this"는 어떻습니까? 이것은 Java 또는 Android입니까? 나는 문맥 상이므로 "this"라고 말하는 것과 같다고 가정 CurrentActivity하지만 클래스 이름 자체에서 "this"가 어떻게 호출되는지 알 수 없습니다. 또한. ".class"는 C #에서 익숙한 리플렉션에 사용되는 것처럼 보이지만 이에 대한 통찰력도 환영합니다.
감사.
일반적으로 만 사용할 수 있습니다 this. 그러나 때로는 this내부 클래스를 참조합니다. 예를 들어 다음과 같습니다.
Button button = (Button)findViewById(R.id.ticket_details_sell_ticket);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// it will be wrong to use only "this", because it would
// reference the just created OnClickListener object
Intent login = new Intent(ClassName.this, Login.class);
startActivityForResult(login, LOGIN_REQUEST);
}
});
한번에 한:
첫 번째 구성은 이것을 this this 라고합니다 . 구문의 목적은 내부 클래스 (일반적으로 익명 내부 클래스) 에 있고 (익명) 내부 클래스가 this아닌 외부 클래스의 클래스 를 참조하려는 경우에 사용됩니다 this. "한정된"은 this모호한 상황에서만 사용할 수 있습니다 . JLS 인용문 "표현식이 클래스 T 또는 T 자체의 내부 클래스가 아닌 클래스 또는 인터페이스에서 발생하면 컴파일 타임 오류입니다."
두 번째 구조는 class literal해당 유형을 나타내는 Class 객체를 참조하는 방법입니다. 어떤 상황에서도 사용할 수 있습니다.
"Classname.this"구문은 내부 클래스를위한 것입니다. "Outerclass"유형의 둘러싸는 인스턴스를 참조하려면 "Outerclass.this"로 수행하십시오.
NextActivity.class는 단순히 "NextActivity"클래스를 설명하는 Class 객체입니다.
NextActivity.class자바 typeof(NextActivity)에서 C #의 의미
ClassName.this 내부 클래스에서 외부 클래스의 현재 인스턴스를 참조하는 데 사용됩니다.
<ClassName>.this
`this '키워드는 중첩 클래스 인스턴스를 참조하기 때문에 중첩 클래스의 현재 인스턴스를 참조하기 위해 중첩 클래스에서 사용됩니다.
공개 수업 Siht {
NestedSiht {클래스
void demoThis () {
System.err.println ( " Siht.this '는 다음의 인스턴스입니다 :"+this' is an instance of: " +
this.getClass().getName());
System.err.println("
Siht.this.getClass (). getName ());
}
}
void demoThis() {
new java.lang.Object() {
void demoThis() {
System.err.println("`this' is an instance of: " +
this.getClass().getName());
System.err.println("`Siht.this' is an instance of: " +
Siht.this.getClass().getName());
}
}.demoThis();
new NestedSiht().demoThis();
}
public static void main(String [] args) {
new Siht().demoThis();
}
}
It's confusing only because when you use "MainActivity.this", it seems that you are referring to the class and not the object. In reality when you use "this" you are always referring to the current object, as the java se documentation states:
https://docs.oracle.com/javase/tutorial/java/javaOO/thiskey.html
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.
It's just syntactically peculiar.
참고URL : https://stackoverflow.com/questions/4080868/using-this-with-class-name
'Programing' 카테고리의 다른 글
| CKEditor와 커스텀 파일 브라우저 / 업 로더를 어떻게 통합 할 수 있습니까? (0) | 2020.07.27 |
|---|---|
| AsyncTask 스레드는 절대 죽지 않습니다 (0) | 2020.07.27 |
| ggplot을 사용하여 축의 숫자 형식을 어떻게 변경합니까? (0) | 2020.07.27 |
| android에서 adjustResize와 adjustPan의 차이점은 무엇입니까? (0) | 2020.07.27 |
| 자바; (0) | 2020.07.27 |