Java创建对象实例的三种方法

Java创建对象实例的三种方法

(2009-11-24 01:01:07)
标签:

杂谈

Java有一下三种方法可以创建对象实例。

1.new

通常都是使用java的关键字new来创建对象实例。

若有一个Something类,则可以通过下面的语句创建Something类的对象实例并指定到变量obj。

Java代码 复制代码
  1. Something somethingNew = new Something();      
  2.   
  3. Something somethingNew = new Something();     
  4.   
  5. Something somethingNew = new Something(); 通过new创建对象实例必须把类名写在原代码里面。  
Something somethingNew = new Something(); Something somethingNew = new Something(); Something somethingNew = new Something(); 通过new创建对象实例必须把类名写在原代码里面。



2.clone

若程序写成如下,则可以根据当前对象(this)建立一个新实例对象(没有调用构造函数).

Java代码 复制代码
  1. public class Something implements Cloneable{       
  2.     private Something obj;       
  3.     public Something cloneSomething()       
  4.     {       
  5.         try {       
  6.             obj = (Something)this.clone();       
  7. //          obj = (Something)clone();       
  8.         } catch (CloneNotSupportedException e) {       
  9.             e.printStackTrace();       
  10.         }       
  11.         return obj;       
  12.                
  13.     }       
  14. }      
  15.   
  16. public class Something implements Cloneable{      
  17.     private Something obj;      
  18.     public Something cloneSomething()      
  19.     {      
  20.         try {      
  21.             obj = (Something)this.clone();      
  22. //          obj = (Something)clone();      
  23.         } catch (CloneNotSupportedException e) {      
  24.             e.printStackTrace();      
  25.         }      
  26.         return obj;      
  27.               
  28.     }      
  29. }     
  30.   
  31. public class Something implements Cloneable{   
  32.     private Something obj;   
  33.     public Something cloneSomething()   
  34.     {   
  35.         try {   
  36.             obj = (Something)this.clone();   
  37. //          obj = (Something)clone();   
  38.         } catch (CloneNotSupportedException e) {   
  39.             e.printStackTrace();   
  40.         }   
  41.         return obj;   
  42.            
  43.     }   
  44. } 如果需要复制上面的那个obj指向的对象实例时,调用somethingNew.cloneSomething()方法就ok了。  
public class Something implements Cloneable{ private Something obj; public Something cloneSomething() { try { obj = (Something)this.clone(); // obj = (Something)clone(); } catch (CloneNotSupportedException e) { e.printStackTrace(); } return obj; } } public class Something implements Cloneable{ private Something obj; public Something cloneSomething() { try { obj = (Something)this.clone(); // obj = (Something)clone(); } catch (CloneNotSupportedException e) { e.printStackTrace(); } return obj; } } public class Something implements Cloneable{ private Something obj; public Something cloneSomething() { try { obj = (Something)this.clone(); // obj = (Something)clone(); } catch (CloneNotSupportedException e) { e.printStackTrace(); } return obj; } } 如果需要复制上面的那个obj指向的对象实例时,调用somethingNew.cloneSomething()方法就ok了。

但是为什么不直接使用somethingNew.clone()呢?

JDK中Object# clone()方法的原型是:
protected native Object clone() throws CloneNotSupportedException;

方法修饰符是protected,而不是public。这种访问的不可见性使得我们对Object#clone()方法不可见。

所以,必需重写Object的clone方法后才能使用。

Java代码 复制代码
  1. public Object clone()throws CloneNotSupportedException       
  2. {       
  3.     Something obj;       
  4.     obj= (Something)super.clone();       
  5.     return obj;       
  6. }      
  7.   
  8. public Object clone()throws CloneNotSupportedException      
  9. {      
  10.     Something obj;      
  11.     obj= (Something)super.clone();      
  12.     return obj;      
  13. }     
  14.   
  15. public Object clone()throws CloneNotSupportedException   
  16. {   
  17.     Something obj;   
  18.     obj= (Something)super.clone();   
  19.     return obj;   
  20. }   
public Object clone()throws CloneNotSupportedException { Something obj; obj= (Something)super.clone(); return obj; } public Object clone()throws CloneNotSupportedException { Something obj; obj= (Something)super.clone(); return obj; } public Object clone()throws CloneNotSupportedException { Something obj; obj= (Something)super.clone(); return obj; }

值得注意的是:如果需要使用clone方法,必需实现java.lang.Cloneable接口,否则会抛出java.lang.CloneNotSupportedException。

另外clone方法所做的的操作是直接复制字段的内容,换句话说,这个操作并不管该字段对应的对象实例内容。

像这样字段对字段的拷贝(field to field copy)就成为"浅拷贝",clone方法所做的正是"浅拷贝".



3.newInstance

利用java.lang.Class类的newInstance方法,则可根据Class对象的实例,建立该Class所表示的类的对象实例。

创建Something类的对象实例可以使用下面的语句(这样需要一个已经存在的对象实例)。

Java代码 复制代码
  1.   
  2. somethingNew.getClass().newInstance().       
  3.   
  4. somethingNew.getClass().newInstance().      
  5.   
  6. somethingNew.getClass().newInstance(). 或者使用下面的语句(只需要存在相应的.class文件即可)   
  7.   
  8. Something instance = (Something) Class.forName("cn.softkid.test.Something").newInstance();      
  9.   
  10. Something instance = (Something) Class.forName("cn.softkid.test.Something").newInstance();     
  11.   
  12. Something instance = (Something) Class.forName("cn.softkid.test.Something").newInstance(); 如果包下不存在相应.class文件,则会抛出ClassNotFoundException。  
somethingNew.getClass().newInstance(). somethingNew.getClass().newInstance(). somethingNew.getClass().newInstance(). 或者使用下面的语句(只需要存在相应的.class文件即可) Something instance = (Something) Class.forName("cn.softkid.test.Something").newInstance(); Something instance = (Something) Class.forName("cn.softkid.test.Something").newInstance(); Something instance = (Something) Class.forName("cn.softkid.test.Something").newInstance(); 如果包下不存在相应.class文件,则会抛出ClassNotFoundException。


注意 :newInstance创建对象实例的时候会调用无参的构造函数,所以必需确保类中有无参数的构造函数,否则将会

抛出java.lang.InstantiationException异常。无法进行实例化。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章