java创建对象的四种方式

来源:互联网 发布:ae数字矩阵 特效 编辑:程序博客网 时间:2024/05/20 06:24

方式一:使用new方式

package athena.unitop.createobj;/** * 需要被创建的类 *  * @author yufu *  */public class User {    private String uname;    private int age;    public void test(){    }    public User() {        System.out.println("对象被创建了");    }    public User(String uname, int age) {        super();        this.uname = uname;        this.age = age;    }    public String getUname() {        return uname;    }    public void setUname(String uname) {        this.uname = uname;    }    public int getAge() {        return age;    }    public void setAge(int age) {        this.age = age;    }}

测试类:

package athena.unitop.test;import athena.unitop.createobj.User;/** * 测试类 * 第一种方式:通过new来创建对象 * @author yufu *  */public class Test {    public static void main(String[] args) {        User user1=new User();        System.out.println(user1);    }}

方式二:使用反射

package athena.unitop.test;import athena.unitop.createobj.User;/** * 测试类 * 第二种方式:通过反射来创建对象 * @author yufu *  */public class Test {    @SuppressWarnings("unchecked")    public static void main(String[] args) throws Exception {        Class clazz=Class.forName("athena.unitop.createobj.User");        User user2=(User) clazz.newInstance();        System.out.println(user2);    }}

方式三:使用反序列化

使用这种方式的前提是要创建的类必须实现序列化接口.

package athena.unitop.createobj;import java.io.Serializable;/** * 需要被创建的类 *  * @author yufu *  */public class User implements Serializable {    private static final long serialVersionUID = 2682170144580892911L;    private String uname;    private int age;    public void test() {        System.out.println("User类的普通方法");    }    public User() {        System.out.println("对象被创建了");    }    public User(String uname, int age) {        super();        this.uname = uname;        this.age = age;    }    public String getUname() {        return uname;    }    public void setUname(String uname) {        this.uname = uname;    }    public int getAge() {        return age;    }    public void setAge(int age) {        this.age = age;    }}

测试代码:

package athena.unitop.test;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import athena.unitop.createobj.User;/** * 第三种方式:使用反序列化来创建对象 * 第一步:使用序列化将对象写入磁盘; * 第二步:使用反序列化将写入磁盘的对象读出来. *  * @author yufu *  */public class Test2 {    public static void main(String[] args) throws Exception {        /**         * 首先序列化写入对象         */        User user = new User();        FileOutputStream fos = new FileOutputStream("e:/a.txt");        ObjectOutputStream oos = new ObjectOutputStream(fos);        oos.writeObject(user);        oos.close();        fos.close();        /**         * 然后反序列化读出对象         */        FileInputStream fis = new FileInputStream("e:/a.txt");        ObjectInputStream ois = new ObjectInputStream(fis);        User user2 = (User) ois.readObject();        System.out.println(user2);        ois.close();        fis.close();    }}

方式四:使用克隆

注意:使用克隆方式需要实现Cloneable接口,并重写父类的clone()方法,Cloneable 接口是一个空接口,clone()方法并不是Cloneable 接口的方法.

package athena.unitop.createobj;/** * 需要被创建的类 *  * @author yufu *  */public class User2 implements Cloneable {    private String uname;    private int age;    public void test() {        System.out.println("User类的普通方法");    }    public User2() {        System.out.println("对象被创建了");    }    public User2(String uname, int age) {        super();        this.uname = uname;        this.age = age;    }    public String getUname() {        return uname;    }    public void setUname(String uname) {        this.uname = uname;    }    public int getAge() {        return age;    }    public void setAge(int age) {        this.age = age;    }    /**     * 需要重写clone方法,父类(Object)的clone()方法是protected,改为public     */    @Override    public Object clone() throws CloneNotSupportedException {        return super.clone();    }}

测试代码:

package athena.unitop.test;import athena.unitop.createobj.User2;/** * 第四种方式:使用克隆的方式 *  * @author yufu *  */public class Test3 {    /**     * @param args     * @throws CloneNotSupportedException     */    public static void main(String[] args) throws CloneNotSupportedException {        User2 user1 = new User2();        User2 user2 = (User2) user1.clone();        System.out.println(user2);    }}