public class Person implements Cloneable, Serializable {
public void hello(){
System.out.println("hello world");
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
public class Demo {
public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException, CloneNotSupportedException, IOException {
Person person = new Person();
person.hello();
Class aclass = Class.forName("zixue.Person");
Person person1 = (Person) aclass.newInstance();
person1.hello();
Class bclass = Class.forName("zixue.Person");
Constructor constructor = bclass.getConstructor();
Person person2 = (Person) constructor.newInstance();
person2.hello();
Person person3 = (Person) person.clone();
person3.hello();
Person person4 = new Person();
File f = new File("person.obj");
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(f));
oos.writeObject(person4);
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(f));
Person person5 = (Person)ois.readObject();
person5.hello();
}
}