建议结合上一篇Java类的加载和初始化一起阅读,将帮助你理解:
- 什么是Class,什么是class
- Java虚拟机启动和类的加载过程
- 对象的创建方式和初始化顺序
对象的创建方式
下面结合代码介绍几种对象的创建方式:
//待实例化的类
public class Worker implements Cloneable,Serializable {
private static final long serialVersionUID = 1L;
private String name;
private Integer age;
public Worker()
{
this.name = "";
this.age = 0;
}
public Worker(String name,Integer age)
{
this.name = name;
this.age = age;
}
public void work()
{
System.out.println(name +"is working");
}
public Worker clone()
{
Worker worker = null;
try {
return (Worker) super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
return worker;
}
}
方式1: 直接使用new的方式,不使用参数
public static Worker createWorker() {
System.out.println("直接使用new的方式,不使用参数");
return new Worker();
}
方式2: 使用new方式,带参数
public static Worker createWorker(String name, int age) {
System.out.println("使用new方式,带参数");
return new Worker(name, age);
}
方式3: 使用反射机制,不带参数的 newInstance() 方法
public static Worker createWorker1() {
Class clazz = null;
Worker worker = null;
try {
clazz = Class.forName("com.code.loader.Worker");
worker = (Worker) clazz.newInstance();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
System.out.println("使用反射机制,不带参数的newInstance()方法");
return worker;
}
方式4: 使用反射机制 , Constructor的 newInstance方法
public static Worker createWorker2() {
Worker worker = null;
try {
Class clazz = null;
clazz = Class.forName("com.code.loader.Worker");
// 获取不带参数的构造器
Constructor constructor = clazz.getConstructor();
// 使用构造器创建对象
worker = (Worker) constructor.newInstance();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
System.out.println("使用反射机制,Constructor的newInstance方法");
return worker;
}
方式5: 使用反射机制 :带参数的构造函数创建新对象
public static Worker createWorker3(String name, Integer age) {
Worker worker = null;
try {
Class clazz = null;
clazz = Class.forName("com.code.loader.Worker");
// 获取带参数的构造器
Constructor constructor = clazz.getConstructor(name.getClass(),
age.getClass());
// 使用构造器创建对象
worker = (Worker) constructor.newInstance(name, age);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
System.out.println("使用反射机制,带参数的构造函数创建新对象");
return worker;
}
方式6: 使用序列化和反序列化创建对象
public static Worker createWorker4(String name, Integer age){
Worker person = new Worker();
person.setName(name);
person.setAge(age);
//序列化
OutputStream outputStream = new FileOutputStream("person.txt");
ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);
objectOutputStream.writeObject(person);
outputStream.close();
objectOutputStream.close();
//反序列化
InputStream inputStream = new FileInputStream("person.txt");
ObjectInputStream objectInputStream = new ObjectInputStream(inputStream);
return (Worker) objectInputStream.readObject();
}
方式7: 使用对象的clone方法,创建对象
public static Worker createWorker5(Worker worker) {
System.out.println("使用对象的复制,创建对象");
return (Worker) worker.clone();
}
对象的初始化顺序
结合代码,分析对象的初始化顺序
public class Test {
public static void main(String[] args) {
Child child = new Child();
}
}
class Father {
public static String fatherStr1 = "fatherStr1(静态字段初始化值)";
public String fatherStr2 = "fatherStr2(字段初始化值)";
static {
System.out.println("父类静态代码块:" + fatherStr1);
fatherStr1 = "fatherStr1(静态代码块赋值)";
}
{
System.out.println("父类构造代码块:" + fatherStr2);
fatherStr2 = "fatherStr2(构造代码块赋值)";
}
public Father() {
System.out.println("父类构造函数块:" + fatherStr2);
fatherStr2 = "fatherStr2(构造函数赋值)";
}
}
class Child extends Father {
public static String childStr1 = "childStr1(静态字段初始化值)";
public String childStr2 = "childStr2(字段初始化值)";
static {
System.out.println("子类静态代码块:" + childStr1);
childStr1 = "childStr1(静态代码块赋值)";
}
{
System.out.println("子类构造代码块:" + childStr2);
childStr2 = "childStr2(构造代码块赋值)";
}
public Child() {
System.out.println("子类构造函数:" + childStr2);
childStr2 = "childStr2(构造函数赋值)";
}
}
/*输出结果:
父类静态代码块:fatherStr1(静态字段初始化值)
子类静态代码块:childStr1(静态字段初始化值)
父类构造代码块:fatherStr2(字段初始化值)
父类构造函数块:fatherStr2(构造代码块赋值)
子类构造代码块:childStr2(字段初始化值)
子类构造函数:childStr2(构造代码块赋值)*/
总结如下,其中静态字段和静态代码块属于类的初始化过程
1、父类静态字段初始化,静态代码块初始化
2、子类静态字段初始化,子类静态代码块初始化
3、父类普通字段初始化,普通代码块初始化
4、父类构造函数
5、子类普通字段初始化,普通代码块初始化
6、子类构造函数
网友评论