Java基礎學習筆記(十)—— 對象
I'm the type of person,if you ask me a question, and I don't konw the answer,I'm gonna tell you that I don't konw.But I bet you what: I know how to find the answer, and I'll find the answer.
| @Author:TTODS
對象的創建
一個對象的創建包括兩個部分,即聲明和實例化。
對象的聲明
type name;
type是引用類型,即類,接口,數組。
例如我們在聲明一個String類型的對象時,String myName
.
注意,對於只聲明而沒有實例化的對象,我們只是對其分配了一個引用,暫時還沒有爲對象分配內存空間。
對象的實例化
對象的實例化包括兩個部分:①爲對象分配內存空間,②對象的初始化。
一般我們使用new
關鍵字爲對象分配內存空間,然後調用構造對象的方法初始化對象
String myName;
myName = new String("TTODS.");
package first;
public class Test{
public static void main(String[] args) {
String[] strArray = new String[10];
for(String str:strArray) {
System.out.println(str);
}
}
}
空對象
對於沒有實例化,僅聲明的對象,它的值爲null;
例如:一個未初始化的String
數組,其中的元素的值默認爲null
package first;
public class Test{
public static void main(String[] args) {
String[] strArray = new String[10]; //聲明一個String 數組,但沒有對數組中的元素進行初始化
for(String str:strArray) {
System.out.println(str);
}
}
}
輸出
null
null
null
null
null
null
null
null
null
null
又例如下例中,我們定義了一個Animal類,Animal有一個成員變量name
,且其默認的構造方法中沒有對name
進行初始化。然後我們在Test.java文件中創建一個Animal對象obj
,由於obj.name
始終沒有初始化,所以obj.name
的值爲null
.有時我們需要使用obj==null
來判斷一個對象obj
是否已初始化。
//first包內的Animal.java文件
package first;
public class Animal{
String name;
public Animal() {};
}
//first包內的Test.java文件
package first;
public class Test{
public static void main(String[] args) {
Animal obj = new Animal();
System.out.println(obj.name);
if(obj.name==null)
System.out.println("The name of obj is null");
}
}
輸出
null
The name of obj is null
構造方法
構造方法是對象實例化時用來初始化一個對象的屬性值的方法,,java中構造方法有三大特點:
- 構造方法名必須與類名相同。
- 構造方法沒有任何返回值,包括void。
- 構造方法只能與
new
運算符結合使用。
默認構造方法
默認構造方法的方法體中內無任何語句,也就是不能初始化成員變量,那麼這些成員變量就會使用與它類型對應的默認值。默認構造方法
package first;
public class Animal{
String name;
float weight;
public Animal() {};//默認的無參構造方法
}
值得注意的是,當在類中沒有構造方法時,java虛擬機會自動提供一個無參的默認構造函數。如下例中,我們在Animal
類中沒有寫構造方法。但我們仍可以運行Test.java的代碼,此時就是使用了無參的默認構造方法。
//first包內的Animal.java文件
package first;
public class Animal{
String name;
float weight;
}
//first包內的Test.java文件
package first;
public class Test{
public static void main(String[] args) {
Animal obj = new Animal();
}
}
但是,當類中存在其他的構造方法時,不會自動生成無參的構造方法,若要使用必須寫出。如下例中,我們在Animal
中寫了一個構造方法public Animal(String n,float w)
,但是沒有寫無參的構造方法,然後Test.java文件中使用了new Animal()
來構造一個Animal
對象,會報錯構造函數 Animal()未定義
。
package first;
public class Animal{
String name;
float weight;
public Animal(String n,float w) {}
}
package first;
public class Test{
public static void main(String[] args) {
Animal obj = new Animal();
}
}
構造方法的重載
重載方法的名字都是一樣的,在下面的例子中,我們爲Animal
類寫了4個構造方法。
package first;
public class Animal{
String name;
float weight;
//默認的無參構造方法
public Animal() {
this.name = "暫未命名的神祕生物" ;
this.weight = 0.0f; //用0.0f 代表重量未知
}
//知道名字和重量
public Animal(String n,float w) {
this.name = n;
this.weight = w;
}
//只知道重量,不知道名字
public Animal(float w) {
this.weight = w;
}
//只知道名字,不知道重量
public Animal(String n) {
this.name = n;
}
}
package first;
public class Test{
public static void main(String[] args) {
//神祕動物1:一切未知
Animal mysteriousAnimal1 = new Animal();
System.out.printf("mysteriousAnimal1: name:%S , weight:%.2fkg\n",mysteriousAnimal1.name,mysteriousAnimal1.weight);
//神祕動物2:只知道重量,不知道名字
Animal mysteriousAnimal2 = new Animal(20.0f);
System.out.printf("mysteriousAnimal2: name:%S , weight:%.2fkg\n",mysteriousAnimal2.name,mysteriousAnimal2.weight);
//一隻 名叫 "小T" ,重 4.0kg的動物。
Animal myDog = new Animal("小T",4.0f);
System.out.printf("myDog: name:%S , weight:%.2fkg\n",myDog.name,myDog.weight);
//一隻只知道 名字叫 "小花",卻不知道重量的動物
Animal yourDog = new Animal("小花");
System.out.printf("yourDog: name:%S , weight:%.2fkg\n",yourDog.name,yourDog.weight);
}
}
輸出:
mysteriousAnimal1: name:暫未命名的神祕生物 , weight:0.00kg
mysteriousAnimal2: name:暫未命名的神祕生物 , weight:20.00kg
myDog: name:小T , weight:4.00kg
yourDog: name:小花 , weight:0.00kg
this關鍵字
我們在上面的例子中有用到this關鍵字,java中this關鍵字和c++中的一樣,我們可以只用this
來調用對象的成員變量和方法,例如
this.name;
this.toString();
再談構造方法(this和super語句在構造方法中的使用)
我們在一個類中使用多個構造方法時,可以在一個構造方法內使用this
調用該類其他的構造方法。但是如果該構造方法中還有其他的語句,this語句
必須是該構造方法中的第一句代碼。
package first;
public class Animal{
String name;
float weight;
//知道名字和重量
public Animal(String n,float w) {
this.name = n;
this.weight = w;
}
//只知道重量,不知道名字
public Animal(float w) {
this("暫未命名的神祕生物",w);
}
//只知道名字,不知道重量
public Animal(String n) {
this(n,0.0f);
}
//默認的無參構造方法
public Animal() {
this("暫未命名的神祕生物",0.0f);
}
}
類似於this
語句在構造方法中的使用方式,我們也可以使用super
關鍵字調用父類中的構造方法,當然super
語句也只能是該構造方法的第一句,這裏就不多演示了。
還有一點,類的構造方法同樣可以使用public
,protected
,private
來修飾,訪問級別與普通方法一樣。
對象銷燬
java語言對象是由垃圾回收器(Garbage Collection)自動收集並釋放的,程序員不必關心釋放的細節。當一個對象的引用不存在時,該對象就會被釋放