springboot

JAVA创建对象的五种方式

2019-08-26  本文已影响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;
    }
 
}
上一篇下一篇

猜你喜欢

热点阅读