Java基礎(chǔ)第二篇方法與數(shù)據(jù)成員
上一篇文章Java基礎(chǔ) 從HelloWorld到面向?qū)ο?/a>
1、調(diào)用同一對象的數(shù)據(jù)成員
方法可以調(diào)用該對象的數(shù)據(jù)成員。比如下面我們給Human類增加一個getHeight()的方法。該方法返回height數(shù)據(jù)成員的值:
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,它用來指代對象自身
。當我們創(chuàng)建一個aPerson實例時,this就代表了aPerson這個對象。this.height指aPerson的height。
this是隱性參數(shù)(implicit argument
)。方法調(diào)用的時候,盡管方法的參數(shù)列表并沒有this,Java都會“默默”的將this參數(shù)傳遞給方法。
(有一些特殊的方法不會隱性傳遞this,我們會在以后見到)
this并不是必需的,上述方法可以寫成:
/** * accessor */ int getHeight() { return height; }
Java會自己去判斷height是類中的數(shù)據(jù)成員。但使用this會更加清晰。
我們還看到了/** comments */的添加注釋的方法。
2、方法的參數(shù)列表
Java中的方法定義與C語言中的函數(shù)類似。Java的方法也可以接收參數(shù)列表(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為傳遞的參數(shù)。在類定義中,說明了參數(shù)的類型(int)。在具體的方法內(nèi)部,我們可以使用該參數(shù)。該參數(shù)只在該方法范圍,即growHeight()內(nèi)有效。
在調(diào)用的時候,我們將10傳遞給growHeight()。aPerson的高度增加了10。
3、調(diào)用同一對象的其他方法
在方法內(nèi)部,可以調(diào)用同一對象的其他方法。在調(diào)用的時候,使用this.method()的形式。我們還記得,this指代的是該對象。所以this.method()指代了該對象自身的method()方法。
比如下面的repeatBreath()函數(shù):
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; }
為了便于循環(huán),在repeatBreath()方法中,我們聲明了一個int類型的對象i。i的作用域限定在repeatBreath()方法范圍內(nèi)部。
(這與C語言函數(shù)中的自動變量類似)
4、數(shù)據(jù)成員初始化
在Java中,數(shù)據(jù)成員有多種初始化(initialize
)的方式。比如上面的getHeight()的例子中,盡管我們從來沒有提供height的值,但Java為我們挑選了一個默認
初始值0。
基本類型的數(shù)據(jù)成員的默認初始值:
數(shù)值型: 0
布爾值: false
其他類型: null
我們可以在聲明數(shù)據(jù)成員同時,提供數(shù)據(jù)成員的初始值。這叫做顯式初始化(explicit initialization)。顯示初始化的數(shù)值要硬性的寫在程序中:
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; }
這里,數(shù)據(jù)成員height的初始值為175,而不是默認的0了。
Java中還有其它初始化對象的方式,我將在以后介紹。
5、總結(jié)
return
this, this.field, this.method()
默認初始值,顯式初始化
到此這篇關(guān)于Java基礎(chǔ) 方法與數(shù)據(jù)成員的文章就介紹到這了,更多相關(guān)Java 方法與數(shù)據(jù)成員內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
ThreadLocal數(shù)據(jù)存儲結(jié)構(gòu)原理解析
這篇文章主要為大家介紹了ThreadLocal數(shù)據(jù)存儲結(jié)構(gòu)原理解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-10-10Java正確實現(xiàn)一個單例設(shè)計模式的示例
今天小編就為大家分享一篇關(guān)于Java正確實現(xiàn)一個單例設(shè)計模式的示例,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-01-01springboot項目整合druid數(shù)據(jù)庫連接池的實現(xiàn)
這篇文章主要介紹了springboot項目整合druid數(shù)據(jù)庫連接池的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-04-04