Java基礎教程之對象的方法與數(shù)據(jù)成員
此外,我們還了解了類(class)。同一類的對象屬于相同的類型(type)。我們可以定義類,并使用該定義來產(chǎn)生對象。
我們進一步深入到對象。了解Java中方法與數(shù)據(jù)成員的一些細節(jié)。
調(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 */的添加注釋的方法。
方法的參數(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。
調(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ù)中的自動變量類似)
數(shù)據(jù)成員初始化
在Java中,數(shù)據(jù)成員有多種初始化(initialize)的方式。比如上面的getHeight()的例子中,盡管我們從來沒有提供height的值,但Java為我們挑選了一個默認初始值0。
基本類型的數(shù)據(jù)成員的默認初始值:
1.數(shù)值型: 0
2.布爾值: false
3.其他類型: 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中還有其它初始化對象的方式,我將在以后介紹。
總結(jié)
return
this, this.field, this.method()
默認初始值,顯式初始化
相關文章
Quarkus集成open api接口使用swagger ui展示
這篇文章主要為大家介紹了Quarkus集成open?api接口使用swagger?ui的展示示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步2022-02-02RabbitMq中channel接口的幾種常用參數(shù)詳解
這篇文章主要介紹了RabbitMq中channel接口的幾種常用參數(shù)詳解,RabbitMQ 不會為未確認的消息設置過期時間,它判斷此消息是否需要重新投遞給消費者的唯一依據(jù)是消費該消息的消費者連接是否己經(jīng)斷開,需要的朋友可以參考下2023-08-08