1、通過 new語句進行實例化一個對象:
例如:
public class Test{
public static void main(String[] args){
Test t = new Test();
}
}
2、通過反射機制創建對象
例如:
public class Test{
public static void main(String[] args){
Class c = Class.forName("Test");
Test test = (Test)c.newInstance();
}
}
3、通過clone()方法創建一個對象,但是這個類要先進行實現Cloneable接口;
public class Test implements Cloneable {
public static void main(String[] args) throws CloneNotSupportedException {
Test o1 = new Test();
Test o2 = (Test) o1.clone();
System.out.println(o1 == o2);//結果爲 false
System.out.println(o1.equals(o2));//結果爲false ,Test類沒有重寫equals方法
}
}
4、通過反序列話的方式創建對象(過幾天遇到了補上);