Java 生成对象的方式

1. Java分为显示和隐式生成对象,下面介绍四种显示生成对象的方式

     ● 用new语句创建对象
     ● 运用反射手段,调用java.lang.Class 或者 java.lang.reflect.Constructor 类的newInstance()实例方法
     ● 调用对象的clone()方法
     ● 运用序列化手段,调用java.io.ObjectInputStream 对象的 readObject()方法.

package com.weizhi.object;

import java.io.Serializable;

/**
 * @ClassName CreateInstance
 * @Description TODO
 * @Author weizhi2018
 * @Date 2013-8-14 下午07:39:14
 * 
 */

public class CreateInstance implements Cloneable , Serializable{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String name;
	private String state;

	public CreateInstance(String name,String state) {
		System.out.println("Constructor CreateInstance");
		this.name = name;
		this.state = state;
	}

	/**
	 * 克隆一个对象
	 */
	public CreateInstance clone() {
		CreateInstance instance = null;
		try {
			instance = (CreateInstance) super.clone();
		} catch (CloneNotSupportedException e) {
			e.printStackTrace();
		}
		return instance;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getState() {
		return state;
	}

	public void setState(String state) {
		this.state = state;
	}
}

package com.weizhi.object;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/**
 * @ClassName CreateObject
 * @Description TODO
 * @Author weizhi2018
 * @Date 2013-8-14 下午07:38:54
 * 
 */

public class CreateObject {

	/**
	 * @Title main
	 * @Description TODO
	 * @Author weizhi2018
	 * @param @param args
	 * @return void
	 * @throws
	 */

	public static void main(String[] args) {
		CreateInstance instanceByNew = createByNew();
		CreateInstance instanceByClone = createByClone(instanceByNew);
		CreateInstance instanceByReflect = createByReflect();
		CreateInstance instanceBySerialization = CreateInstanceBySerialization();
		System.out.println("instanceByNew name:" + instanceByNew.getName());
		System.out.println("instanceByClone name:" + instanceByClone.getName());
		System.out.println("instanceByReflect name:"
				+ instanceByReflect.getName());
		System.out.println("instanceBySerialization name:"
				+ instanceBySerialization.getName());
	}

	/**
	 * @Title createByNew
	 * @Description 通过new方式创建
	 * @Author weizhi2018
	 * @param @return
	 * @return CreateInstance
	 * @throws
	 */
	private static CreateInstance createByNew() {
		String name = "weizhi";
		String state = "true";
		return new CreateInstance(name, state);
	}

	/**
	 * @Title createByClone
	 * @Description 通过Clone方式创建一个对象
	 * @Author weizhi2018
	 * @param @param instance
	 * @param @return
	 * @return CreateInstance
	 * @throws
	 */
	private static CreateInstance createByClone(CreateInstance instance) {
		return instance.clone();
	}

	/**
	 * @Title createByReflect
	 * @Description 通过反射方式创建一个对象
	 * @Author weizhi2018
	 * @param @return
	 * @return CreateInstance
	 * @throws
	 */
	private static CreateInstance createByReflect() {
		CreateInstance instance = null;
		try {
			instance = (CreateInstance) Class.forName(
					"com.weizhi.object.CreateInstance").newInstance();
			instance.setName("weizhiReflect");
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		return instance;
	}

	/**
	 * @Title CreateInstanceBySerialization 
	 * @Description 通过反序列化方式实例化一个对象
	 * @Author weizhi2018 
	 * @param @return     
	 * @return CreateInstance     
	 * @throws
	 */
	private static CreateInstance CreateInstanceBySerialization() {
		String filename = "E:\\Test\\InstanceBySerialization.txt";
		CreateInstance instanceByStream = null;
		try {
			ObjectOutputStream objOut = new ObjectOutputStream(
					new FileOutputStream(filename));
			CreateInstance instance = new CreateInstance(
					"InstanceBySerialization", "true");
			// 将对象以流的形式存储
			objOut.writeObject(instance);

			ObjectInputStream objIn = new ObjectInputStream(
					new FileInputStream(filename));
			// 将数据流读取为对象
			instanceByStream = (CreateInstance) objIn.readObject();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		return instanceByStream;
	}

}


2.重载了构造函数之后,就不能用默认构造函数实例化对象

原因是:如果没有构造函数,编译器就自动添加一个默认构造函数;当写了构造函数之后,编译器就不会添加默认构造函数,相当于没有这个函数了,就会报错。

你可能感兴趣的:(Java 生成对象的方式)