java中創建對象方法有:1.使用new關鍵字創建;2.使用Class類創建;3.使用構造函數創建;4.使用反序列化創建;
java中創建對象方法有以下幾種
1.使用new關鍵字創建
public class Test {
public static void main(String[] args) {
Test t1 = new Test();
Test t2 = new Test("張三");
}
}
2.使用Class類創建
public static void main(String[] args) throws Exception {
String className = "org.b3log.solo.util.Test";
Class clasz = Class.forName(className);
Test t = (Test) clasz.newInstance();
}
3.使用構造函數創建
Constructor
constructor = Student.class.getInstance(); Student stu = constructor.newInstance();
4.使用反序列化創建
ObjectInputStream in = new ObjectInputStream (new FileInputStream("data.obj"));
Student stu3 = (Student)in.readObject();