一、this關(guān)鍵字主要有三個應(yīng)用:
(1)this調(diào)用本類中的屬性,也就是類中的成員變量;
(2)this調(diào)用本類中的其他方法;
(3)this調(diào)用本類中的其他構(gòu)造方法,調(diào)用時要放在構(gòu)造方法的首行。
關(guān)鍵字this用于指代當前的對象。因此,類內(nèi)部可以使用this作為前綴引用實例成員;
this()代表了調(diào)用另一個構(gòu)造函數(shù),至于調(diào)用哪個構(gòu)造函數(shù)根據(jù)參數(shù)表確定。this()調(diào)用只能出現(xiàn)在構(gòu)造函數(shù)的第一行。
當在內(nèi)部類中使用關(guān)鍵字this,指的就是內(nèi)部類的對象, 為了訪問外層類對象,就可以使用外層類名.this來訪問,一般也只在這種情況下使用這種
示例代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
public class Activity extends Activity { public void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); /* 設(shè)置顯示main.xml布局 */ setContentView(R.layout.main); /* findViewById(R.id.button)取得布局main.xml中的button */ Button button = (Button) findViewById(R.id.button); /* 監(jiān)聽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); /* 關(guān)閉當前的Activity */ Activity. this .finish(); } }); } } |
以上所述是小編給大家介紹的Java關(guān)鍵字 ClassName.this中類名.this的理解的相關(guān)介紹,希望對大家有所幫助!