Java创建对象的4种方式

使用new关键字

通过new关键字直接在堆内存上创建对象,这样很方便的调用对象的有参和无参的构造函数.

Student stu1 = new Student("lihua");

Class反射调用

使用Java中反射特性,来进行对象的创建。使用Class类的newInstance方法可以调用无参的构造器来创建对象,如果是有参构造器,则需要使用ClassforName方法和Constructor来进行对象的创建。

Class stuClass = Class.forName("Student");
Constructor constructor = stuClass.getConstructor(String.class);
Student stu2 = (Student) constructor.newInstance("李四");

使用Clone方法

使用Clone的方法:无论何时我们调用一个对象的clone方法,JVM就会创建一个新的对象,将前面的对象的内容全部拷贝进去,用clone方法创建对象并不会调用任何构造函数。要使用clone方法,我们必须先实现Cloneable接口并实现其定义的clone方法。

try
{
    Student stu3 = (Student) stu1.clone();
    System.out.println(stu3);
}
catch (CloneNotSupportedException e)
{
    e.printStackTrace();
}

使用序列化

一个对象实现了Serializable接口,就可以把对象写入到文件中,并通过读取文件来创建对象。

String path = Test.class.getClassLoader().getResource("").getPath();
String objectFilePath = path + "out.txt";

ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream(objectFilePath));
objectOutputStream.writeObject(stu2);

ObjectInput objectInput = new ObjectInputStream(new FileInputStream(objectFilePath));
Student stu4 = (Student) objectInput.readObject();

示例代码

Student对象,实现CloneableSerializable接口

import java.io.Serializable;

/**
 * Created by wzj on 2017/11/3.
 */
public class Student implements Cloneable,Serializable
{
    private String name;

    public Student(String name)
    {
        this.name = name;
    }

    /**
     * @return a clone of this instance.
     * @throws CloneNotSupportedException if the object's class does not
     *                                    support the {@code Cloneable} interface. Subclasses
     *                                    that override the {@code clone} method can also
     *                                    throw this exception to indicate that an instance cannot
     *                                    be cloned.
     * @see Cloneable
     */
    @Override
    protected Object clone() throws CloneNotSupportedException
    {
        return super.clone();
    }
}

测试类

import java.io.*;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
 * Created by wzj on 2017/11/2.
 */
public class Test
{
    public static void main(String[] args) throws Exception
    {
        //1、第一种方式是通过new
        Student stu1 = new Student("lihua");
        System.out.println(stu1);

        //2、通过java反射,静态方式
        Class stuClass = Class.forName("Student");
        Constructor constructor = stuClass.getConstructor(String.class);
        Student stu2 = (Student) constructor.newInstance("李四");

        System.out.println(stu2);

        //3、通过clone实现,必须实现Cloneable接口
        try
        {
            Student stu3 = (Student) stu1.clone();
            System.out.println(stu3);
        }
        catch (CloneNotSupportedException e)
        {
            e.printStackTrace();
        }

        //4、通过对象流,必须实现Serializable
        String path = Test.class.getClassLoader().getResource("").getPath();
        String objectFilePath = path + "out.txt";

        ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream(objectFilePath));
        objectOutputStream.writeObject(stu2);

        ObjectInput objectInput = new ObjectInputStream(new FileInputStream(objectFilePath));
        Student stu4 = (Student) objectInput.readObject();
        System.out.println(stu4);
    }


}
  • 6
    点赞
  • 49
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

dmfrm

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值