java实例化对象的方式

一、Java中创建(实例化)对象的五种方式
  1、用new语句直接创建对象,这是最常见的创建对象的方法。

  2、通过工厂方法返回对象,如:String str = String.valueOf(23); 

  3、运用反射手段,调用java.lang.Class或者java.lang.reflect.Constructor类的newInstance()实例方法。如:Object obj = Class.forName("java.lang.Object").newInstance(); 

  4、调用对象的clone()方法。

  5、通过I/O流(包括反序列化),如运用反序列化手段,调用java.io.ObjectInputStream对象的 readObject()方法。

二.实例说明:

 创建 Fruit(水果)实体类 

package com.shine.demo.object;

import java.io.Serializable;

@SuppressWarnings("serial")
public class Fruit implements Serializable, Comparable<Fruit>{
	
	/**
	 * id.
	 */
	private Long id;
	
	/**
	 * 名称.
	 */
	private String name;
	
	/**
	 * 价格.
	 */
	private Double price;
	
	public Fruit() {
		super();
	}

	public Fruit(Long id, String name, Double price) {
		super();
		this.id = id;
		this.name = name;
		this.price = price;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

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

	public Double getPrice() {
		return price;
	}

	public void setPrice(Double price) {
		this.price = price;
	}
	
	@Override
	public String toString() {
		return "id为:" + this.getId() + ",的:"+ this.getName() +"的价格为:"+this.getPrice();
	}

	@Override
	public int compareTo(Fruit otherFruit) {
		
		if(!(this.id.equals(otherFruit.id)))
			return this.id.compareTo(otherFruit.id);
		else if(!(this.name.equals(otherFruit.name)))
			return this.name.compareTo(otherFruit.name);
		else if(this.price.equals(otherFruit.price))
			return this.price.compareTo(otherFruit.price);

		return 0;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		result = prime * result + ((price == null) ? 0 : price.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (!(obj instanceof Fruit))
			return false;
		
		Fruit other = (Fruit) obj;
		
		return this.id.equals(other.id) && this.name.equals(other.name) && this.price.equals(other.price);
	}
	

}

测试说明 : 下面主要对1/3方式进行说明

package com.shine.demo.object;

public class ObjOpen {

	public static void main(String[] args) {
		
		/**方法一 : new语句直接创建对象 */
		Fruit fruit = new Fruit(1L,"苹果",4.3);
		System.out.println(fruit.toString());
		
		
		
		/**方法三 :运用反射手段,调用java.lang.Class或者java.lang.reflect.Constructor类的newInstance()实例方法。
		 *         如:Object obj = Class.forName("java.lang.Object").newInstance(); 
		 **/
		try {
			Fruit fruit2 = (Fruit) Class.forName("com.shine.demo.object.Fruit").newInstance();
			fruit2.setId(2L);
			fruit2.setName("香蕉");
			fruit2.setPrice(2.6);
			System.out.println(fruit2);
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		
	}

}

三、结果:  

 
posted @ 2016-07-26 10:18  独具匠心  阅读(19975)  评论(0编辑  收藏  举报