Java中不通過構造方法創建對象的方法總結


    我們常說,Java是一種面向對象的語言,因而在Java中幾乎所有的操作都離不開對象。而在Java語言中,最常見的創建對象的方法是通過對類構造器的調用,除此之外,其實還有下面幾種可以創建對象的方法。

1)通過反射機制來創建對象;

class Person{
     String name="Jack";
     public Person(){
           System.out.println("construct");
    }
    public String toString(){return name;}  
}

public  class Test{
     public static void main(String[] args){
            Class classType;
            try{
                  classType=Class.forname("Person");
                  Person p = (Person)classType.newInstance();
                 System.out.println(p);
            }cathch(Exception e){
                      e.printStackTrace();
            }
     }  
}

程序的運行結果為:

    construct

    Jack

2)調用對象的clone方法,需要以下幾個步驟才能使用clone方法:

(1)實現clone的類首先需要繼承Cloneable接口實質上是一個標識接口,沒有任何的接口方法,這一點和序列化接口Serializable()很類似。

(2)在類中重寫Object類的clone方法。

(3)在clone方法中調用super.clone()。無論clone類的繼承結構是什么,super.clone()都會直接或間接的調用Java.long.Object類中的clone()方法。

實例代碼如下:

class Obj implement Cloneable{
     private int aInt=0;
     public Obj(){
           System.out.println("construct");
     }
     public int getAint(){return aInt;}
     public void changeInt(){this.aInt=1;    }
     public Object clone(){
          Object o=null;
           try{
             o=(Obj)super.clone();
     }catch(CloneNotSuppertedException e){
             e.printStackTrace();
     }
       return 0;
     }
     }
     public class Test{
          public static void main(String[] args){
                Obj a = new Obj();
                Obj b = (Obj)a.clone();
                b.changeInt();
                System.out.println("a:"+a.getAInt());
                System.out.println("b:"+b.getAInt());
     }
     }

程序的運行結果為:

    construct

     a:0

     b:1

從以上的程序運行可以看出,在調用a.clone()方法時,系統創建了新的對象,但是沒有調用構造方法。

3)通過反序列化的方式創建對象,實例代碼如下:

       import java.io.FileInputStream;
       import java.io.FileOutputStream;
       import java.io.ObjectInputStream;
       import java.io.ObjectOutputStream;
       import java.io.Serializable;
       public class Person implement Serilalizable{
              private String name;
              public Person(){
                           this.name="lili";
                           System.out.println("Construct");
               }
              public Stream toString(){return this.name;}
              public static void main(String args[]){
                            Person p = new People();
                            System.out.println(p);
                            ObjectOutputStream  oos=null;
                            ObjectInputStream  ois = null;
                           try{
                                FileOutputStream  fos =new 
                             FileOutputStream("perpke.out");
                              oos=new ObjectOutputStream(fos);
                              oos.writeObject(p);
                              oos.close(0);
                          } catch(Exception ex){}
                          People pl;
                          try{
                                FileInputStream  fis = new 
                                FileInputStream("perple.out");
                                ois = new ObjectInputStream(fis);
                                p1=(People)ois.readObject();
                                System.out.println(p);
                                 if(p!=p1)
                                      System.out.println("two different 
                                        objecrt")
                                 ois.close();
                          }catch(Exception ex){}
               }
               }

程序的運行結果為:

construct 

lili

lili

two fifferent object


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM