美文网首页springboot
JAVA创建对象的五种方式

JAVA创建对象的五种方式

作者: 一个忙来无聊的人 | 来源:发表于2019-08-26 15:02 被阅读0次

创建对象的五种方式

  1. 使用new关键字 } → 调用了构造函数
  2. 使用Class类的newInstance方法 } → 调用了构造函数
  3. 使用Constructor类的newInstance方法 } → 调用了构造函数
  4. 使用clone方法 } → 没有调用构造函数
  5. 使用反序列化 } → 没有调用构造函数

对象代码样例

public class HelloWorld {

    private String name;

    private String sex;
    
    public HelloWorld(String name, String sex) {
        this.name = name;
        this.sex = sex;
    }

    public HelloWorld() {
    }

    public String getName() {
        return name;
    }

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

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }
}

一、使用new关键字

这是最常见也是最简单的创建对象的方式,可以调用任意的构建函数,无参或有参

public class TestHelloWorld {
    // 使用new 关键字创建对象--通过构造函数
    public HelloWorld getHelloWorld1() {
      // 无参方式创建对象
      // HelloWorld helloWorld = new HelloWorld();
      // 有参方式创建对象 
        HelloWorld helloWorld = new HelloWorld("张三","男");
        return helloWorld;
    }
}

二、使用class类的newInstance方法创建对象

如下两个样例就是通过class类的newInstance方法创建对象
注意,该方法必须要对象有一个 无参 的构造函数,否则会抛出异常

public class TestHelloWorld {
    // 使用class类的 newInstance()方法创建对象
    public HelloWorld getHelloWorld() throws ClassNotFoundException, IllegalAccessException, InstantiationException {
        HelloWorld helloWorld = (HelloWorld) Class.forName("com.example.demo.controller.HelloWorld").newInstance();
        return helloWorld;
    }
    // 使用class类的 newInstance()方法创建对象
    public HelloWorld getHelloWorld2() throws IllegalAccessException, InstantiationException {
        HelloWorld helloWorld = HelloWorld.class.newInstance();
        return helloWorld;
    }
}

三、使用Constructor类的newInstance()方法

该方法和方法二类似,java.lang.reflect.Constructor类里也有一个newInstance方法可以创建对象。我们可以通过这个newInstance方法调用有参数的和私有的构造函数

public class TestHelloWorld {
    // 使用Constructor类的newInstance()方法
    public HelloWorld getHelloWorld() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
        // 方式一、通过无参方式创建对象
        Constructor<HelloWorld> constructor = HelloWorld.class.getConstructor();
        HelloWorld helloWorld = constructor.newInstance();

        //方式二、 通过有参方式创建对象
        Constructor<HelloWorld> constructor2 = HelloWorld.class.getConstructor(String.class,String.class);
        HelloWorld helloWorld1 = constructor2.newInstance("张三", "男");
        return helloWorld1;
    }

}

四、使用clone方法

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

// 类需要实现Cloneable 接口
public class HelloWorld implements Cloneable{

    private String name;

    public String getName() {
        return name;
    }

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

  // 类需要重写clone()接口
    @Override
    protected Object clone() throws CloneNotSupportedException{
        return super.clone();
    }
}

通过clone方法进行对象创建

public class TestHelloWorld {
    // 通过clone的方式来创建
    public HelloWorld getHelloWorld() throws CloneNotSupportedException {
        HelloWorld helloWorld = new HelloWorld();
        HelloWorld clone = (HelloWorld) helloWorld.clone();
        return clone;
    }
}

五、通过对象反序列化的方式来创建

将对象保存到硬盘上

public class TestHelloWorld {
    // 对象序列化
    public void saveHelloWorld() throws IOException {
        HelloWorld helloWorld = new HelloWorld();
        helloWorld.setName("张三");
        helloWorld.setSex("M");
        FileOutputStream fileOutputStream = new FileOutputStream("hello.txt");
        ObjectOutputStream ops = new ObjectOutputStream(fileOutputStream);
        ops.writeObject(helloWorld);
        ops.close();
        fileOutputStream.close();
        System.out.println("helloworld序列化完成");
    }
}

讲文件反序列化获取对象

public class TestHelloWorld {
    // 对象反序列化
    public HelloWorld getHelloWorld() throws IOException, ClassNotFoundException {
        FileInputStream fileInputStream = new FileInputStream("hello.txt");
        ObjectInputStream ois = new ObjectInputStream(fileInputStream);
        HelloWorld helloWorld = (HelloWorld) ois.readObject();
        System.out.println("对象反序列化完成");
        ois.close();
        fileInputStream.close();
        return helloWorld;
    }
 
}

相关文章

  • 16、Java创建对象有几种方式?

    Java创建对象有几种方式? java中提供了以下四种创建对象的方式: 1、new创建新对象; 2、通过反射机制;...

  • java 对象的内存布局和大小计算

    创建java对象的方式 java对象的创建有多种,最简单的是new XXClass,还可以通过反射,xx.clo...

  • Java创建对象的方式

    原先了解的Java创建对象有四种方式: (1)用new 语句创建对象,这是最常用的创建对象方法。 (2)运用反射手...

  • Java 创建对象的方式

    Java中有5种创建对象的方式,如下所示: 1.使用new关键字 这是最常见也是最简单的创建对象的方式了。通过这种...

  • Java创建对象的方式

    前言 HI,欢迎来到裴智飞的《每周一博》。今天是十一月第二周,我给大家简单分享一下Java创建一个对象的几种方式。...

  • java 创建对象的方式

    1: new2:Constructor反射3:Test.class.newInstance() 须有默认构造函数4...

  • Java创建对象的方式

    ·使用New关键字 ·使用反射的Class类的newInstance()方法: ·使用反射的Constructor...

  • java基本功16课:(5)表达式中的陷阱

    1.字符串陷阱 1.1 java创建对象的常见方式如下: 通过new调用构造器创建Java对象。 通过Class对...

  • 2.3.1java对象创建过程

    对象创建的5种方式 普通java对象创建过程步骤 碰到new指令创建对象时,检查这个new指定的参数在常量池中是否...

  • 设计模式-工厂模式

    在Java语言中,我们通常由以下几种创建对象的方式:(1)使用new关键字直接创建对象(2)通过反射机制创建对象(...

网友评论

    本文标题:JAVA创建对象的五种方式

    本文链接:https://www.haomeiwen.com/subject/skbrectx.html