一、this關鍵字主要有三個應用:
(1)this調用本類中的屬性,也就是類中的成員變數;
(2)this調用本類中的其他方法;
(3)this調用本類中的其他構造方法,調用時要放在構造方法的首行。
關鍵字this用於指代當前的對象。因此,類內部可以使用this作為首碼引用執行個體成員;
this()代表了調用另一個建構函式,至於調用哪個建構函式根據參數表確定。this()調用只能出現在建構函式的第一行。
當在內部類中使用關鍵字this,指的就是內部類的對象, 為了訪問外層類對象,就可以使用外層類名.this來訪問,一般也只在這種情況下使用這種
範例程式碼:
public class Activity extends Activity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); /* 設定顯示main.xml布局 */ setContentView(R.layout.main); /* findViewById(R.id.button)取得布局main.xml中的button */ Button button = (Button) findViewById(R.id.button); /* 監聽button的事件資訊 */ button.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) { /* 建立一個Intent對象 */ Intent intent = new Intent(); /* 指定intent要啟動的類 */ intent.setClass(Activity.this</span>, Activity.class); /* 啟動一個新的Activity */ startActivity(intent); /* 關閉當前的Activity */ Activity.this.finish(); } }); } }
以上所述是小編給大家介紹的Java關鍵字 ClassName.this中類名.this的理解的相關介紹,希望對大家有所協助!