首页 > 代码库 > Several ways to instantiate a new instance

Several ways to instantiate a new instance

package com.fish.study.instance;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
 * Created by Gan on 2017/7/1 0001.
 */
public class PersonBeanInstantiate {

    public static void main(String[] args){
        PersonBean personBean = null;
        // Use new
        personBean = newBean();
        // Use Constructor
        constructorBean();
        // Use classForname(Reflect)
        fornameBean();
        // Use Clone
        cloneBean(personBean);
        // Use Serializable
        serializableBean(personBean);
    }

    private static PersonBean newBean(){
        PersonBean personBean = null;
        try {
            printStatus(InstantiateType.NEW, Status.BEGIN);
            personBean = new PersonBean();
            printStatus(InstantiateType.NEW,Status.FINISHED);
        }catch (Exception e){
        }finally {
            return personBean;
        }
    }

    private static void constructorBean() {
        try {
            printStatus(InstantiateType.CONSTRUR,Status.BEGIN);
            ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
            Class clazz = classLoader.loadClass(PersonBean.class.getName());
            Constructor constructor = clazz.getConstructors()[0];
            constructor.newInstance();
            printStatus(InstantiateType.CONSTRUR,Status.FINISHED);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }

    private static void serializableBean(PersonBean personBean) {
        try {
            File file = new File("PersonBean.out");
            ObjectOutputStream oout =  new ObjectOutputStream(new FileOutputStream(file));
            oout.writeObject(personBean);
            oout.close();


            printStatus(InstantiateType.SERIALIZE,Status.BEGIN);
            ObjectInputStream oin = new ObjectInputStream(new FileInputStream(file));
            Object newPerson = oin.readObject();
            oin.close();
            printStatus(InstantiateType.SERIALIZE,Status.FINISHED);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    private static void cloneBean(PersonBean personBean) {
        try {
            printStatus(InstantiateType.CLONE,Status.BEGIN);
            PersonBean personBeanClone =  (PersonBean) personBean.clone();
            printStatus(InstantiateType.CLONE,Status.FINISHED);
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
    }

    private static void fornameBean() {
        try {
            printStatus(InstantiateType.FORNAME,Status.BEGIN);
            PersonBean personBean = (PersonBean)Class.forName(PersonBean.class.getName()).newInstance();
            printStatus(InstantiateType.FORNAME,Status.FINISHED);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }
    }

    /**
     * Created by Administrator on 2017/7/1 0001.
     */
    public static class PersonBean implements java.io.Serializable,Cloneable {
        public PersonBean(){
            System.out.println("invoke constructor");
        }

        @Override
        protected Object clone() throws CloneNotSupportedException {
            return super.clone();
        }

        public String getName() {
            return name;
        }

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

        public String getWeight() {
            return weight;
        }

        public void setWeight(String weight) {
            this.weight = weight;
        }

        public String getAddress() {
            return address;
        }

        public void setAddress(String address) {
            this.address = address;
        }

        private String name;
        private String weight;
        private String address;
    }


    private static void printStatus(InstantiateType instantiateType,Status status){
        System.out.println(String.format("Use %-9s to instatiate new instance.....  %-9s  .....",instantiateType.toString(),status.toString()));
    }

    private enum Status{
        BEGIN,FINISHED;
    }

    private  enum InstantiateType{
        NEW,CONSTRUR,FORNAME,CLONE,SERIALIZE;
    }
}

  

Several ways to instantiate a new instance