程序员

JAVA创建对象的四种方式

2020-06-28  本文已影响0人  向远方_1f71

1.直接new (无需多言)

2.使用反射机制创建

/**

*

* @author eastw

*

*        待创建的类

*/

public class Hello {

public void sayWorld() {

System.out.println("Hello world!");

}

}

/**

*

* @author eastw

*

*        使用反射的机制创建对象 Class类的newInstance方法

*/

public class Two {

public static void main(String[] args)

throws ClassNotFoundException, InstantiationException, IllegalAccessException {

// 获取类对象

Class<?> helloClass = Class.forName("com.eastw.mode.Hello");

// newInstance创建对象

Hello hello = (Hello) helloClass.newInstance();

hello.sayWorld();

}

}

import java.lang.reflect.Constructor;

import java.lang.reflect.InvocationTargetException;

/**

*

* @author eastw

*

*        使用Constructor类的newInstance方法

*/

public class Three {

public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, SecurityException,

InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {

// 获取类对象

Class<?> helloClass = Class.forName("com.eastw.mode.Hello");

// 获取构造器

Constructor<?> constructor = helloClass.getConstructor();

// 使用newInstance

Hello hello = (Hello) constructor.newInstance();

hello.sayWorld();

}

}

3.使用clone 生成新的对象

/**

*

* @author eastw

*

*        待创建的类

*/

public class HelloClone implements Cloneable {

public void sayWorld() {

System.out.println("Hello world!");

}

/**

* 把这个方法重写一下就行,什么都不写

*/

@Override

protected Object clone() throws CloneNotSupportedException {

return super.clone();

}

}

/**

*

* @author eastw

*

*        采用clone clone时,需要已经有一个分配了内存的源对象,创建新对象时,首先应该分配一个和源对象一样大的内存空间。

*

*        要调用clone方法需要实现Cloneable接口,由于clone方法是protected的,所以修改Hello类。

*/

public class Four {

public static void main(String[] args) {

HelloClone hello1 = new HelloClone();

try {

HelloClone h2 = (HelloClone) hello1.clone();

h2.sayWorld();

} catch (CloneNotSupportedException e) {

e.printStackTrace();

}

}

}

4.采用序列化机制

import java.io.Serializable;

/**

*

* @author eastw

*

*        待创建的类

*/

public class HelloSerializable implements Serializable {

/**

*

*/

private static final long serialVersionUID = 1L;

public void sayWorld() {

System.out.println("Hello world!");

}

}

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.ObjectInputStream;

import java.io.ObjectOutputStream;

/**

*

* @author eastw

*

*        采用序列化机制

*/

public class Five {

private static ObjectOutputStream oos;

private static ObjectInputStream ois;

public static void main(String[] args) {

HelloSerializable hello = new HelloSerializable();

// 准备一个文件用于存储该对象的信息

File file = new File("hello.obj");

try {

FileOutputStream fos = new FileOutputStream(file);

oos = new ObjectOutputStream(fos);

FileInputStream fis = new FileInputStream(file);

ois = new ObjectInputStream(fis);

// 序列化对象,写入到磁盘中

oos.writeObject(hello);

// 反序列化对象

HelloSerializable newHello = (HelloSerializable) ois.readObject();

// 测试方法

newHello.sayWorld();

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (ClassNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

上一篇 下一篇

猜你喜欢

热点阅读