Java學習筆記之this的作用

來源:互聯網
上載者:User

標籤:

?

調用類中變數的寫法實際上應該是:對象名.變數名

1.this代表調用該成員的對象,通常對象名被省略,即省略了this.但是在有同名變數參與時不可省略。如下面的情形:

public void setNum(String num)
{
this.num = num;
}

?

2.當在同一個類中的一個建構函式中調用另一個建構函式時,就可以使用this來直接調用。但是在使用時,含有this的這句話必須放在代碼塊的第一行。如下面的情形:

public Time(long totaltime)//以特定時間(ms)構造time對象的構造方法

{

this(totaltime / 1000, totaltime / 60000, totaltime / 3600000);//調用以特定時,分,秒為參數的構造方法,傳入轉化後的流失時間

}

?

public Time(long totalsecond, long totalminute, long totalhour)

{

/*將已流失的時間轉化為當前的時間*/

currsecond = totalsecond % 60;

currminute = totalminute % 60;

currhour = totalhour % 24;

}

Java學習筆記之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.