java对象实例化的方式

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java对象实例化的方式相关的知识,希望对你有一定的参考价值。

java对象实例化的方式有以下几种:
1、使用new
2、工厂模式
3、反射
4、clone()方法
5、反序列化方式

/**

  • 实现Cloneable和Serializable接口
  • */
    public class Book implements Cloneable, Serializable {
    private static final long serialVersionUID = 1L;

    private Integer serialNum; // 序列号
    private String name; // 图书名称

    public Book() {
    System.out.println("默认构造函数");
    }

    public Book(Integer serialNum, String name) {
    System.out.println("带参数构造函数");
    this.serialNum = serialNum;
    this.name = name;
    }

    public int getSerialNum() {
    return serialNum;
    }
    public void setSerialNum(int serialNum) {
    this.serialNum = serialNum;
    }
    public String getName() {
    return name;
    }
    public void setName(String name) {
    this.name = name;
    }

    @Override
    public String toString() {
    if (name == null) {
    return "书籍属性还未填写";
    }

    return serialNum + " : " + name;

    }

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

class BookFactory {
public static Book getInstance(Integer serialNum, String name) {
return new Book(serialNum, name);
}
}

public class Instantiation {

// 1.通过new实例化对象
public static Book newMode() {
    System.out.println("============================================================");
    System.out.println("通过new实例化对象");
    Book book = new Book(1, "大道扬帆");
    return book;
}

// 2.通过工厂方式实例化对象,其本质还是new,但将实例化的细节交给了工厂来处理,屏蔽了和业务代码无关的功能,一定程度上实现了和业务代码的解耦
public static Book factoryMode() {
    System.out.println("============================================================");
    System.out.println("通过工厂方式实例化对象");
    return BookFactory.getInstance(2, "大道扬帆");
}

// 3.通过反射方式实例化对象
@SuppressWarnings("rawtypes")
public static Book reflectMode() throws Exception {
    System.out.println("============================================================");
    System.out.println("通过反射方式实例化对象");
    Class[] parameterTypes = new Class[] {Integer.class, String.class};
    Constructor constructor = Book.class.getConstructor(parameterTypes);
    Book book = (Book) constructor.newInstance(3, "大道扬帆");

    return book;
}

// 3-1.通过反射方式实例化对象还有一种方式,但本质上其实和上面的一样,但需要类本身有无参构造函数
public static Book reflectAnotherMode() throws Exception {
    System.out.println("============================================================");
    System.out.println("通过反射方式实例化对象的另外一种方式");
    Book book = (Book) Class.forName("instantiation.Book").newInstance();

    return book;
}

// 4.调用已存在对象的clone()方法实例化对象
public static Book cloneMode(Book book) throws Exception {
    System.out.println("============================================================");
    System.out.println("调用已存在对象的clone()方法实例化对象");
    Book newBook = (Book) book.clone();
    System.out.println("原对象和克隆对象是同一个对象吗?" + (book == newBook));
    return newBook;
}

// 5.对象序列化后可以通过反序列化创建对象
public static Book unSerializedMode() throws Exception {
    System.out.println("============================================================");
    System.out.println("对象序列化后可以通过反序列化创建对象");
    Book book = new Book(5, "大道扬帆");

    ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream(new File("D:/book.obj")));
    objectOutputStream.writeObject(book);

    ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream(new File("D:/book.obj")));
    Book newBook = (Book) objectInputStream.readObject();

    System.out.println("原对象和反序列化的对象是同一个吗?" + (book == newBook));

    return newBook;
}

public static void main(String[] args) throws Exception {
    System.out.println(Instantiation.newMode());
    System.out.println(Instantiation.factoryMode());
    System.out.println(Instantiation.reflectMode());
    System.out.println(Instantiation.reflectAnotherMode());

    Book book = new Book(4, "大道扬帆");
    System.out.println(Instantiation.cloneMode(book));

    System.out.println(Instantiation.unSerializedMode());
}

}

看精彩玄幻世界,尽在《大道扬帆》https://book.qidian.com/info/1012993779

以上是关于java对象实例化的方式的主要内容,如果未能解决你的问题,请参考以下文章

Java类的实例化的初始化过程

Python_对象(实例)

JAVA虚拟机系列三-类加载过程双亲委派模型对象实例化过程

Java Web漏洞清单

js对象实例化的常见三种方式

java构造方法和方法的区别