2014-01-28 3 views
1

다른 클래스의 클래스에 액세스해야한다면 해당 클래스의 객체를 만듭니다. 그러나 괄호 안에 this 키워드를 사용하는 이유는 무엇입니까?
누군가 도와 드릴 수 있습니까?android에서 다른 클래스의 클래스에 액세스

public class SQLView extends Activity 
{ 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.sqlview); 
     TextView tv=(TextView) findViewById(R.id.tvSQLinfo); 
     HotOrNot info=new HotOrNot(this); 
     info.open(); String data=info.getData(); 
     info.close(); tv.setText(data); 
    } 
} 
+4

질문에 대한 설명을 추가하십시오. – Simulant

+0

공용 클래스 SQLView 활동 연장 { @Override 보호 공극에서 onCreate (번들 savedInstanceState) {// TODO \t 방법 스텁 \t super.onCreate (savedInstanceState) 자동 생성; \t setContentView (R.layout.sqlview); \t TextView tv = (TextView) findViewById (R.id.tvSQLinfo); \t HotOrNot 정보 = 새로운 HotOrNot (this); \t info.open(); \t 문자열 데이터 = info.getData(); \t info.close(); \t tv.setText (data); } – user3222270

+0

여기 HotOrNot은 액세 스할 객체이고 그 객체가 만들어진 클래스입니다. – user3222270

답변

1

this은 항상 현재 클래스의 개체를 참조합니다.

부모 클래스와 자식 클래스에 같은 이름을 가진 두 개의 메서드가 있고 해당 시간에 자식 클래스의 메서드에 액세스하려는 경우 this.methodName()을 사용하여 자식 클래스의 메서드에 액세스하려는 것을 나타낼 수 있습니다.

예 :

public class parentClass 
{ 
    public void printMsg() 
    { 
     System.out.println ("This is Parent Class"); 
    } 
} 


public class childClass extends parentClass 
{ 
    public void printMsg() 
    { 
     System.out.println ("This is child class"); 
    } 

    public static void main (String args[]) 
    { 
     ChildClass cc = new ChildClass(); 
     cc.printMsg(); // This will print Parent class's printMsg() method 
     this.printMsg(); // This will print child class's printMsg() method 

    } 
} 
0

"this는"현재 클래스의 객체를 의미합니다. 이 대신에 getApplicationContext()을 사용할 수도 있습니다. 또는 다음을 수행 할 수도 있습니다.

public class MainActivity extends Activity 
{ 
    Context context=this; 
    public void onCreate(...) 
    { 
    // when you would like to accessing one class from present class then you can also use "context" 
    } 
} 
+0

선생님 나는 데이터베이스에 보관되는 세 가지 옵션을 포함하는 퀴즈의 응용 프로그램을 만들고 있습니다. 그들 중 하나는 올바른 옵션입니다. 사용자가 라디오 버튼을 누를 때 그는 어떻게 올바른 것을 눌렀습니까? ? 나는 모든 옵션에 대한 radiobuttons의 isChecked 메서드를 사용하여 만들었지 만 300 가지 이상의 질문에 대해 구현하기 때문에 데이터베이스 측면에서 다른 대안이 있습니다 ... – user3222270

+0

@ user3222270에서 도와주세요. 그러나 귀하의 문제는 귀하의 의견과 다릅니다. 내 대답이 너를 도울 까? 아직도 당신은 받아 들여지지 않거나 + 1 .... 코멘트 문제에 대해 나와 개인적으로 이야기 할 수 있습니다. –

관련 문제