获取对象的几种方式与构造函数Constructor的调用

本文详细解析了Java中获取对象的四种方式:new、反射、反序列化和克隆,并通过代码示例展示了构造函数在不同场景下的调用情况。包括新创建对象时父类构造体是否被调用、反序列化过程中对象构造体是否被调用等关键信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.获取对象的方式

1.获取对象的方式有new,反射,反序列化和克隆。

2.构造函数Constructor的调用

1.获取对象时构造函数调用示意:

方式父类构造体是否被调用该对象构造体是否被调用
newYY
反射YY
反序列化YN
克隆NN

2.代码

public class Animal {

    public Animal() {
        System.out.println("Animal Constructor called");
    }
}
public class Person extends Animal implements Serializable, Cloneable{

    public Person() {
        System.out.println("Person Constructor called");
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        // TODO Auto-generated method stub
        return super.clone();
    }
}
public static void main(String[] args) {

        System.out.println("----------------new start");
        Person person1 = new Person();
        System.out.println("----------------new end\n");


        System.out.println("----------------reflect start");
        Class c = Person.class;
        try {
            Person person2 = (Person) c.newInstance();
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("----------------reflect end\n");

        System.out.println("----------------serializable start");
        //将对象写入文件
        File file = new File("E://test.txt");
        FileOutputStream fileOutputStream = null;
        ObjectOutputStream objectOutputStream = null;
        try {
            if (file.exists()) {
                file.delete();
            }
            fileOutputStream = new FileOutputStream(file);
            objectOutputStream = new ObjectOutputStream(fileOutputStream);
            objectOutputStream.writeObject(person1);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (objectOutputStream != null) {
                    objectOutputStream.close();
                    objectOutputStream = null;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                if (fileOutputStream != null) {
                    fileOutputStream.close();
                    fileOutputStream = null;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        // 将对象从文件中读出来
        FileInputStream fileInputStream = null;
        ObjectInputStream objectInputStream = null;
        try {
            fileInputStream = new FileInputStream(file);
            objectInputStream = new ObjectInputStream(fileInputStream);
            Person Person3 = (Person) objectInputStream.readObject();
        }catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (objectInputStream != null) {
                    objectInputStream.close();
                    objectInputStream = null;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                if (fileInputStream != null) {
                    fileInputStream.close();
                    fileInputStream = null;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (file.exists()) {
                file.delete();
            }
        }

        System.out.println("----------------serializable end\n");

        System.out.println("----------------clone start");
        try {
            Person person4 = (Person) person1.clone();
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("----------------clone end\n");

    }

运行结果:

----------------new start
Animal Constructor called
Person Constructor called
----------------new end

----------------reflect start
Animal Constructor called
Person Constructor called
----------------reflect end

----------------serializable start
Animal Constructor called
----------------serializable end

----------------clone start
----------------clone end
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值