Java创建对象的方法

方法一:new关键字

定义 Person 类如下:

package com.ph.object;

public class Person {
    private String name;
    private Integer age;

    public Person(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    public void selfIntroduction() {
        System.out.println("我是" + name + ",今年" + age + "岁。");
    }
}

Test 类如下:

package com.ph.object;

public class Test {
    public static void main(String[] args) {
        // new关键字创建
        Person liuBei = new Person("刘备", 50);

        System.out.println(liuBei);
        liuBei.selfIntroduction();
    }
}

输出:

com.ph.object.Person@4554617c
我是刘备,今年50岁。

方法二:反射

Person 类保持不变, Test 改为:

package com.ph.object;

import java.lang.reflect.Constructor;

public class Test {
    public static void main(String[] args) {
        String className = "com.ph.object.Person";
        Person liBai = null;
        try {
            // 反射创建
            Class<?> personClass = Class.forName(className);
            Constructor<?> personConstructor = personClass.getConstructor(String.class, Integer.class);
            liBai = (Person) personConstructor.newInstance("李白", 60);
        } catch (ReflectiveOperationException e) {
            System.out.println("反射创建对象失败");
        }

        System.out.println(liBai);
        if (liBai != null) {
            liBai.selfIntroduction();
        }
    }
}

输出:

com.ph.object.Person@4554617c
我是李白,今年60岁。

方法三:clone方法

使用 clone 方法,需要被克隆的类实现 Cloneable 接口,并重写 Object 类的 clone 方法。

package com.ph.object;

public class Person implements Cloneable {
    private String name;
    private Integer age;

    public Person(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    public void selfIntroduction() {
        System.out.println("我是" + name + ",今年" + age + "岁。");
    }

    @Override
    public Person clone() {
        Person clone = null;
        try {
            clone = (Person) super.clone();
        } catch (CloneNotSupportedException e) {
            System.err.println("克隆创建对象失败");
        }
        return clone;
    }
}

Test 类改为:

package com.ph.object;

public class Test {
    public static void main(String[] args) {
        Person zhuGe = new Person("诸葛亮", 70);

        System.out.println(zhuGe);
        zhuGe.selfIntroduction();

        // 以上部分是初始条件
        
        // clone创建
        Person cloneZhuGe = zhuGe.clone();

        System.out.println(cloneZhuGe);
        cloneZhuGe.selfIntroduction();
    }
}

输出:

com.ph.object.Person@4554617c
我是诸葛亮,今年70岁。
com.ph.object.Person@74a14482
我是诸葛亮,今年70岁。

方法四:反序列化

使用序列化,需要被序列化的类实现 Serializable 接口,并添加 serialVersionUID 字段。

package com.ph.object;

import java.io.Serializable;

public class Person implements Serializable {
    private static final long serialVersionUID = 1L;

    private String name;
    private Integer age;

    public Person(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    public void selfIntroduction() {
        System.out.println("我是" + name + ",今年" + age + "岁。");
    }
}

Test 改为:

package com.ph.object;

import java.io.*;

public class Test {
    public static void main(String[] args) {
        String pathname = "src/com/ph/object/person.txt";
        File file = new File(pathname);
        // 序列化,将对象写入文件
        try {
            OutputStream os = new FileOutputStream(file);
            ObjectOutputStream oos = new ObjectOutputStream(os);
            oos.writeObject(new Person("典韦", 80));
            os.close();
            oos.close();
        } catch (IOException e) {
            System.err.println("序列化失败");
        }
        // 以上是初始条件

        Person dianWei = null;
        try {
            InputStream is = new FileInputStream(file);
            ObjectInputStream ois = new ObjectInputStream(is);
            dianWei = (Person) ois.readObject();
            is.close();
            ois.close();
        } catch (Exception e) {
            System.err.println("反序列化创建对象失败");
        }

        System.out.println(dianWei);
        if (dianWei != null) {
            dianWei.selfIntroduction();
        }
    }
}

输出:

com.ph.object.Person@15aeb7ab
我是典韦,今年80岁。

参考资料

1、java创建对象的四种方式 - 云–澈
2、一篇文章看懂java反射机制(反射实例化对象-反射获得构造方法,获得普通方法,获得字段属性) - 多情剑客无情剑;

你可能感兴趣的:(文件操作,#,Java,【Java基础】)