Java创建对象的方式你知道几种???

准备工作:首先创建一个学生类。

import java.io.Serializable;

public class Student implements Serializable {
    String name;
    int age;
    int score;

    public Student() {
    }

    public Student(String name, int age, int score) {
        this.name = name;
        this.age = age;
        this.score = score;
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }
}

1.通过new关键字来创建对象。

 Student s1=new Student("张三",15,78);

2.通过反射的构造方法来创建对象。

//反射
        Constructor declaredConstructor = Student.class.getDeclaredConstructor(String.class, int.class, int.class);
        Student stu = declaredConstructor.newInstance("熊爱明", 15, 89);
        System.out.println(stu);

 不懂反射的同学可以看这里:你还不会反射吧,快来吧!!!_明天更新的博客-CSDN博客

 3.通过克隆来创建对象。

import com.sun.xml.internal.messaging.saaj.util.ByteInputStream;
import sun.misc.Unsafe;

import java.io.*;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.lang.reflect.Constructor;

public class Test {
    public static void main(String[] args) throws Throwable {
        //克隆
        Student student=new Student("张三",15,78);
        Object clone = student.clone();
        System.out.println(clone);
        
    }
}

4.通过反序列化来创建对象。(Student类实现Serializable接口)

import com.sun.xml.internal.messaging.saaj.util.ByteInputStream;
import sun.misc.Unsafe;

import java.io.*;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.lang.reflect.Constructor;

public class Test {
    public static void main(String[] args) throws Throwable {
        //反序列化
        byte[] bytes = serialize(student);
        Student s2 = (Student)deserialize(bytes);
        System.out.println(s2);

    }
    private static byte[] serialize(Student student) throws IOException{
        ByteArrayOutputStream baos=new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(baos);
        oos.writeObject(student);
        byte[] bytes = baos.toByteArray();
        return bytes;
    }
    private static Object deserialize(byte[] bytes) throws IOException,ClassNotFoundException{
        ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bytes));
        return ois.readObject();
    }
}

5.通过MethodHandle  API来创建对象。

//MethodHandle API
        MethodHandle constructor = MethodHandles.lookup().findConstructor(Student.class, MethodType.methodType(void.class, String.class, int.class, int.class));
        Student s = (Student)constructor.invoke("李四", 45, 89);
        System.out.println(s);

你可能感兴趣的:(java,开发语言,idea)