淺析Java ClassName.this中類名.this關鍵字的理解_java

來源:互聯網
上載者:User

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.