java建立_java创建对象的四种方式

1.使用new创建对象

使用new关键字创建对象应该是最常见的一种方式,但我们应该知道,使用new创建对象会增加耦合度。无论使用什么框架,都要减少new的使用以降低耦合度。

packageyunche.test;/*** @ClassName: Hello

* @Description: 待创建的类

*@author: yunche

* @date: 2018/08/24*/

public classHello

{public voidsayWorld()

{

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

}

}

packageyunche.test;/*** @ClassName: NewClass

* @Description: 使用new关键字创建对象

*@author: yunche

* @date: 2018/08/24*/

public classNewClass

{public static voidmain(String[] args)

{

Hello h= newHello();

h.sayWorld();

}

}

01bc4eba5aa0b09450befb067764f235.png

2.使用反射的机制创建对象

使用Class类的newInstance方法

Hello类的代码不变,NewClass类的代码如下:

packageyunche.test;/*** @ClassName: NewClass

* @Description: 使用Class类的newInstance方法

*@author: yunche

* @date: 2018/08/24*/

public classNewClass

{public static voidmain(String[] args)

{try{

Class heroClass= Class.forName("yunche.test.Hello");

Hello h=(Hello) heroClass.newInstance();

h.sayWorld();

}catch(ClassNotFoundException e)

{

e.printStackTrace();

}catch(IllegalAccessException e)

{

e.printStackTrace();

}catch(InstantiationException e)

{

e.printStackTrace();

}

}

}

使用Constructor类的newInstance方法

packageyunche.test;importjava.lang.reflect.Constructor;importjava.lang.reflect.InvocationTargetException;/*** @ClassName: NewClass

* @Description: 使用Constructor类的newInstance方法

*@author: yunche

* @date: 2018/08/24*/

public classNewClass

{public static voidmain(String[] args)

{try{//获取类对象

Class heroClass = Class.forName("yunche.test.Hello");//获取构造器

Constructor constructor =heroClass.getConstructor();

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

h.sayWorld();

}catch(NoSuchMethodException e)

{

e.printStackTrace();

}catch(InvocationTargetException e)

{

e.printStackTrace();

}catch(IllegalAccessException e)

{

e.printStackTrace();

}catch(InstantiationException e)

{

e.printStackTrace();

}catch(ClassNotFoundException e)

{

e.printStackTrace();

}

}

}

3.采用clone

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

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

packageyunche.test;/*** @ClassName: Hello

* @Description: 待创建的类

*@author: yunche

* @date: 2018/08/24*/

public class Hello implementsCloneable

{public voidsayWorld()

{

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

}public static voidmain(String[] args)

{

Hello h1= newHello();try{

Hello h2=(Hello)h1.clone();

h2.sayWorld();

}catch(CloneNotSupportedException e)

{

e.printStackTrace();

}

}

}

4.采用序列化机制

使用序列化时,要实现实现Serializable接口,将一个对象序列化到磁盘上,而采用反序列化可以将磁盘上的对象信息转化到内存中。

packageyunche.test;import java.io.*;/*** @ClassName: Serialize

* @Description: 序列化与反序列化对象

*@author: yunche

* @date: 2018/08/24*/

public classSerialize

{public static voidmain(String[] args)

{

Hello h= newHello();//准备一个文件用于存储该对象的信息

File f = new File("hello.obj");try(FileOutputStream fos = newFileOutputStream(f);

ObjectOutputStream oos= newObjectOutputStream(fos);

FileInputStream fis= newFileInputStream(f);

ObjectInputStream ois= newObjectInputStream(fis)

)

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

oos.writeObject(h);//反序列化对象

Hello newHello =(Hello)ois.readObject();//测试方法

newHello.sayWorld();

}catch(FileNotFoundException e)

{

e.printStackTrace();

}catch(IOException e)

{

e.printStackTrace();

}catch(ClassNotFoundException e)

{

e.printStackTrace();

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值