使用new關鍵字方式一使用物件的clone方法
使用反序列化
使用class類的newinstance方法
使用constructor的類的newinstance方法
person lamze = new person("lamze", 18);
方式二
/**
* 使用clone方法建立物件
* 步驟:
* 1、要建立的物件的類實現cloneable介面
* 2、重寫clone方法
*/
person lamze2 =(person) lamze.clone();
方式三
/**
* 使用反序列化建立物件
* 1、實現serializable介面(乙個標記可以序列化的介面)
* 2、使用objectinputstream讀取物件
*/
//反序列化
try(objectinputstream in = new objectinputstream(new fileinputstream("d:/person.obj"))) catch (filenotfoundexception |classnotfoundexception e) catch (ioexception e)
方式四
/**
* 使用class的newinstance方法
* 注意:只能呼叫 類的無參構造器
*/
classclazz = person.class
; try catch (instantiationexception | illegalacces***ception e)
方式五
/**
* 使用constructor的newinstance建立物件
* */
classclzz = person.class;
try catch (illegalacces***ception|invocationtargetexception|instantiationexception e)
} catch (nosuchmethodexception e)
這裡有完整的**
作為blog的第一篇文章,我覺得以這個為題是不錯的選擇了。
java物件的建立過程
以一道例題開始 public class test static public test string str public static int print string str public static void main string args 執行結果是 1 j i 0 n 0 2 構造快...
Java建立物件的順序
建立乙個類 c的物件 1.載入所有得類。2.執行靜態初始化。3.分配記憶體,並初始化為零。4.處理呼叫的c的建構函式的引數。6.處理隱式或者顯式的建構函式 this 或者super 7.初始化c中的變數。8.執行c的 instance initialization 9.執行 呼叫的c的建構函式。cl...
Java物件建立的順序
實踐出真知,上 package test 用來檢測建立順序的類 public class c package test 父類 public class astatic public static c c2 newc 父類 靜態成員變數 public c c newc 父類 普通成員變數 packag...