標籤:
有4種顯式地建立對象的方式:
1.用new語句建立對象,這是最常用的建立對象的方式。
2.運用反射手段,調用java.lang.Class或者java.lang.reflect.Constructor類的newInstance()執行個體方法。
3.調用對象的clone()方法。
4.運用還原序列化手段,調用java.io.ObjectInputStream對象的readObject()方法.
下面示範了用前面3種方式建立對象的過程。
[java] view plaincopyprint?
- public class Customer implements Cloneable{
- private String name;
- private int age;
- public Customer(){
- this("unknown",0);
- System.out.println("call default constructor");
- }
- public Customer(String name,int age){
- this.name=name;
- this.age=age;
- System.out.println("call second constructor");
- }
- public Object clone()throws CloneNotSupportedException{
- return super.clone();
- }
- public boolean equals(Object o){
- if(this==o)return true;
- if(! (o instanceof Customer)) return false;
- final Customer other=(Customer)o;
- if(this.name.equals(other.name) && this.age==other.age)
- return true;
- else
- return false;
- }
- public String toString(){
- return "name="+name+",age="+age;
- }
- public static void main(String args[])throws Exception{
- //運用反射手段建立Customer對象
- Class objClass=Class.forName("Customer");
- Customer c1=(Customer)objClass.newInstance(); //會調用Customer類的預設構造方法
- System.out.println("c1: "+c1); //列印name=unknown,age=0
-
- //用new語句建立Customer對象
- Customer c2=new Customer("Tom",20);
- System.out.println("c2: "+c2); //列印name=tom,age=20
-
- //運用複製手段建立Customer對象
- Customer c3=(Customer)c2.clone(); //不會調用Customer類的構造方法
- System.out.println("c2==c3 : "+(c2==c3)); //列印false
- System.out.println("c2.equals(c3) : "+c2.equals(c3)); //列印true
- System.out.println("c3: "+c3); //列印name=tom,age=20
- }
- }
public class Customer implements Cloneable{private String name;private int age;public Customer(){ this("unknown",0); System.out.println("call default constructor");}public Customer(String name,int age){ this.name=name; this.age=age; System.out.println("call second constructor");}public Object clone()throws CloneNotSupportedException{return super.clone();}public boolean equals(Object o){ if(this==o)return true; if(! (o instanceof Customer)) return false; final Customer other=(Customer)o; if(this.name.equals(other.name) && this.age==other.age) return true; else return false;}public String toString(){return "name="+name+",age="+age;}public static void main(String args[])throws Exception{//運用反射手段建立Customer對象Class objClass=Class.forName("Customer");Customer c1=(Customer)objClass.newInstance(); //會調用Customer類的預設構造方法System.out.println("c1: "+c1); //列印name=unknown,age=0 //用new語句建立Customer對象Customer c2=new Customer("Tom",20);System.out.println("c2: "+c2); //列印name=tom,age=20 //運用複製手段建立Customer對象Customer c3=(Customer)c2.clone(); //不會調用Customer類的構造方法System.out.println("c2==c3 : "+(c2==c3)); //列印falseSystem.out.println("c2.equals(c3) : "+c2.equals(c3)); //列印trueSystem.out.println("c3: "+c3); //列印name=tom,age=20}}
以上程式的列印結果如下:
call second constructor
call default constructor
c1: name=unknown,age=0
call second constructor
c2: name=Tom,age=20
c2==c3 : false
c2.equals(c3) : true
c3: name=Tom,age=20
從以上列印結果看出,用new語句或Class對象的newInstance()方法建立Customer對象時,都會執行Customer類的構造方法,而用對象的clone()方法建立Customer對象時,不會執行Customer類的構造方法。(區別)
除了以上4種顯式地建立對象的方式以外,在程式中還可以隱含地建立對象,包括以下幾種情況:
1.對於java命令中的每個命令列參數,Java虛擬機器都會建立相應的String對象,並把它們組織到一個String數組中,再把該數組作為參數傳給程式入口main(String args[])方法。
2.程式碼中的String類型的直接數對應一個String對象,例如:
[java] view plaincopyprint?
- String s1="Hello";
- String s2="Hello"; //s2和s1引用同一個String對象
- String s3=new String("Hello");
- System.out.println(s1==s2); //列印true
- System.out.println(s1==s3); //列印false
String s1="Hello";String s2="Hello"; //s2和s1引用同一個String對象String s3=new String("Hello");System.out.println(s1==s2); //列印trueSystem.out.println(s1==s3); //列印false
執行完以上程式,記憶體中實際上只有兩個String對象,一個是直接數,由Java虛擬機器隱含地建立,還有一個通過new語句顯式地建立。
3.字串操作符“+”的運算結果為一個新的String對象。例如:
[java] view plaincopyprint?
- String s1="H";
- String s2=" ello";
- String s3=s1+s2; //s3引用一個新的String對象
- System.out.println(s3=="Hello"); //列印false
- System.out.println(s3.equals("Hello")); //列印true
String s1="H";String s2=" ello";String s3=s1+s2; //s3引用一個新的String對象System.out.println(s3=="Hello"); //列印falseSystem.out.println(s3.equals("Hello")); //列印true
4.當Java虛擬機器載入一個類時,會隱含地建立描述這個類的Class執行個體.
java基礎之 建立對象的幾種方式