Java基礎02 方法與資料成員

來源:互聯網
上載者:User

標籤:

Vamei 出處:http://www.cnblogs.com/vamei 歡迎轉載,也請保留這段聲明。謝謝!

 

在Java基礎01 從HelloWorld到物件導向,我們初步瞭解了對象(object)。對象中的資料成員表示對象的狀態。對象可以執行方法,表示特定的動作。

此外,我們還瞭解了類(class)。同一類的對象屬於相同的類型(type)。我們可以定義類,並使用該定義來產生對象。

我們進一步深入到對象。瞭解Java中方法與資料成員的一些細節。

 

調用同一對象的資料成員

方法可以調用該對象的資料成員。比如下面我們給Human類增加一個getHeight()的方法。該方法返回height資料成員的值:

public class Test{    public static void main(String[] args)    {        Human aPerson = new Human();        System.out.println(aPerson.getHeight());    }}class Human{/**     * accessor     */    int getHeight()    {        return this.height;    }    int height;}

我們新增了getHeight方法。這個方法有一個int類型的傳回值。Java中使用return來傳回值。

 

注意this,它用來指代對象自身。當我們建立一個aPerson執行個體時,this就代表了aPerson這個對象。this.height指aPerson的height。

this是隱性參數(implicit argument)。方法調用的時候,儘管方法的參數列表並沒有this,Java都會“默默”的將this參數傳遞給方法。

(有一些特殊的方法不會隱性傳遞this,我們會在以後見到)

 

this並不是必需的,上述方法可以寫成:

    /**     * accessor     */    int getHeight()    {        return height;    }

Java會自己去判斷height是類中的資料成員。但使用this會更加清晰。

我們還看到了/** comments  */的添加註釋的方法。

 

方法的參數列表

Java中的方法定義與C語言中的函數類似。Java的方法也可以接收參數列表(argument list),放在方法名後面的括弧中。下面我們定義一個growHeight()的方法,該方法的功能是讓人的height增高:

public class Test{    public static void main(String[] args)    {        Human aPerson = new Human();        System.out.println(aPerson.getHeight());
aPerson.growHeight(10);
System.out.println(aPerson.getHeight()); }}class Human{/** * accessor */ int getHeight() { return this.height; }
/**
* pass argument
*/
void growHeight(int h) { this.height = this.height + h; }
int height;}

在growHeight()中,h為傳遞的參數。在類定義中,說明了參數的類型(int)。在具體的方法內部,我們可以使用該參數。該參數只在該方法範圍,即growHeight()內有效。

在調用的時候,我們將10傳遞給growHeight()。aPerson的高度增加了10。

 

調用同一對象的其他方法

在方法內部,可以調用同一對象的其他方法。在調用的時候,使用this.method()的形式。我們還記得,this指代的是該對象。所以this.method()指代了該對象自身的method()方法。

 

比如下面的repeatBreath()函數:

public class Test{    public static void main(String[] args)    {        Human aPerson = new Human();        aPerson.repeatBreath(10);    }}class Human{    void breath()    {        System.out.println("hu...hu...");    }   /**    * call breath()    */    void repeatBreath(int rep)    {        int i;        for(i = 0; i < rep; i++) {            this.breath();        }    }    int height;}

為了便於迴圈,在repeatBreath()方法中,我們聲明了一個int類型的對象i。i的範圍限定在repeatBreath()方法範圍內部。

(這與C語言函數中的自動變數類似)

 

資料成員初始化

在Java中,資料成員有多種初始化(initialize)的方式。比如上面的getHeight()的例子中,儘管我們從來沒有提供height的值,但Java為我們挑選了一個預設初始值0。

基本類型的資料成員的預設初始值:

  • 數值型: 0
  • 布爾值: false
  • 其他類型: null

 

我們可以在聲明資料成員同時,提供資料成員的初始值。這叫做顯式初始化(explicit initialization)。顯示初始化的數值要硬性的寫在程式中:

public class Test{    public static void main(String[] args)    {        Human aPerson = new Human();        System.out.println(aPerson.getHeight());    }}class Human{/**     * accessor     */    int getHeight()    {        return this.height;    }    int height = 175;}

這裡,資料成員height的初始值為175,而不是預設的0了。

Java中還有其它初始化對象的方式,我將在以後介紹。

 

總結

return

this, this.field, this.method()

預設初始值,顯式初始化

Java基礎02 方法與資料成員

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.