一、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的理解的相關介紹,希望對大家有所幫助!