java 序列化和反序列化实现克隆

package com.clone.test;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class TestClone{

	public static Hero clone(Hero hero) {
		Hero copy = null;
		try {
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			ObjectOutputStream oos;
			oos = new ObjectOutputStream(baos);
			oos.writeObject(hero);
			//序列化可以写入文件。当然,也可以写入内存,然后在读取出来,这时候就实现了反序列化(克隆)
			ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
			ObjectInputStream ois = new ObjectInputStream(bais);
			copy = (Hero) ois.readObject();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		return copy;
	}
	public static void main(String[] args) {
		
		Hero hero = new Hero("哈撒给", 2500, 18);
		System.out.println("英雄名字 "+(hero.getName() == clone(hero).getName()));
		System.out.println("宠物是否是同一个 "+(hero.getPet() == clone(hero).getPet()));
		System.out.println("英雄的血量相同否 "+(hero.getHP() == clone(hero).getHP()));
		System.out.println("英雄的宠物的名字相同否"+hero.getPet().getName() == clone(hero).getPet().getName());
		System.out.println("英雄的宠物的血量是否相同"+(hero.getPet().getHP() == clone(hero).getHP()));
		
		
	}
}

package com.clone.test;

import java.io.InputStream;
import java.io.Serializable;

public class Hero implements Serializable {

	private String name;//英雄名字
	private int HP;//血量
	private int level;//等级
	private Pet pet = new Pet("宝宝", 20, 1000);//一个英雄拥有的宠物
	private static int MP;//被标注为静态的属性也没有办法被序列化
	private transient InputStream is;//为了方便解释,加入一个没有办法实现serializable接口的对象,这时候就需要将它用transient标记
	private transient int a;//没有意义的一个变量

	// 当某个对象中的属性没有办法序列化,或者不需要将它序列化的时候可以使用关键字transient标记,就可以实现序列化的时候忽略

	public Hero(String name, int hP, int level) {
		super();
		this.name = name;
		HP = hP;
		this.level = level;
	}
	public Hero(String name, int hP, int level, InputStream is,
			int a) {
		super();
		this.name = name;
		HP = hP;
		this.level = level;
		this.is = is;
		this.a = a;
	}


	public String getName() {
		return name;
	}

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

	public int getHP() {
		return HP;
	}

	public void setHP(int hP) {
		HP = hP;
	}

	public int getLevel() {
		return level;
	}

	public void setLevel(int level) {
		this.level = level;
	}
	public static int getMP() {
		return MP;
	}
	public static void setMP(int mP) {
		MP = mP;
	}
	public Pet getPet() {
		return pet;
	}
	public void setPet(Pet pet) {
		this.pet = pet;
	}
	public InputStream getIs() {
		return is;
	}
	public void setIs(InputStream is) {
		this.is = is;
	}
	public int getA() {
		return a;
	}
	public void setA(int a) {
		this.a = a;
	}
}
class Pet implements Serializable{
	private String name;
	private int leval;
	private int HP;
	public String getName() {
		return name;
	}
	
	public Pet(String name, int leval, int hP) {
		super();
		this.name = name;
		this.leval = leval;
		HP = hP;
	}

	public void setName(String name) {
		this.name = name;
	}
	public int getLeval() {
		return leval;
	}
	public void setLeval(int leval) {
		this.leval = leval;
	}
	public int getHP() {
		return HP;
	}
	public void setHP(int hP) {
		HP = hP;
	}
	
}

你可能感兴趣的:(Java)