Java建立對象的六種方法-權當記錄一下

1 簡介

Java是面向對象的編程語言,只要使用它,就須要建立對象。Java建立對象有六種方法,實際經常使用的不會這麼多,這裏權當是記錄一下。java

2 六種方法

(1)使用new關鍵字編程

Pumpkin p1 = new Pumpkin();

(2)反射之Class類newInstance()微信

Pumpkin p2 = Pumpkin.class.newInstance();

(3)反射之Constructor類的newInstance()編程語言

Pumpkin p3 = Pumpkin.class.getDeclaredConstructor().newInstance();

(4)Object對象的clone方法ide

Pumpkin p4 = (Pumpkin) p1.clone();

注意Object類的clone方法是protected的,在Override的時候,能夠改爲public,這樣讓其它全部類均可以調用。函數

注意淺拷貝和深拷貝。code

(5)反序列化對象

ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("object.bin"));
oos.writeObject(p1);
oos.close();

ObjectInputStream ois = new ObjectInputStream(new FileInputStream("object.bin"));
Pumpkin p5 = (Pumpkin) ois.readObject();
ois.close();

必需要實現Serializable接口;接口

須要注意哪些字段可序列化,哪些字段不會被序列化,如何控制;get

注意serialVersionUID的做用;

瞭解Externalizable的不一樣之處。

(6)使用Unsafe類

Field f = Unsafe.class.getDeclaredField("theUnsafe");
f.setAccessible(true);
Unsafe unsafe = (Unsafe) f.get(null);
Pumpkin p6 = (Pumpkin) unsafe.allocateInstance(Pumpkin.class);

不多用的方法,通常不用瞭解這個方法。

3 示例代碼

示例代碼以下:

package com.pkslow.basic;


import sun.misc.Unsafe;

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

public class CreateObject {

    public static class Pumpkin implements Cloneable, Serializable {
        public Pumpkin(){
            System.out.println("Constructor called");
        }
        @Override
        public Object clone() throws CloneNotSupportedException {
            return super.clone();
        }
    }

    public static void main(String[] args) throws IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException, CloneNotSupportedException, IOException, ClassNotFoundException, NoSuchFieldException {

        System.out.println("---start---");
        System.out.println("(1) new");
        Pumpkin p1 = new Pumpkin();

        System.out.println("(2) Class newInstance");
        Pumpkin p2 = Pumpkin.class.newInstance();

        System.out.println("(3) Constructor newInstance");
        Pumpkin p3 = Pumpkin.class.getDeclaredConstructor().newInstance();

        System.out.println("(4) clone");
        Pumpkin p4 = (Pumpkin) p1.clone();

        System.out.println("(5)Serialization");
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("object.bin"));
        oos.writeObject(p1);
        oos.close();

        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("object.bin"));
        Pumpkin p5 = (Pumpkin) ois.readObject();
        ois.close();

        System.out.println("(6) Unsafe");
        Field f = Unsafe.class.getDeclaredField("theUnsafe");
        f.setAccessible(true);
        Unsafe unsafe = (Unsafe) f.get(null);
        Pumpkin p6 = (Pumpkin) unsafe.allocateInstance(Pumpkin.class);

        System.out.println("---end---");
    }
}

輸出結果以下:

---start---
(1) new
Constructor called
(2) Class newInstance
Constructor called
(3) Constructor newInstance
Constructor called
(4) clone
(5)Serialization
(6) Unsafe
---end---

因此會執行構造函數的有:new關鍵字、兩種反射;

不會執行構造函數的有:clone、序列化、Unsafe類。

4 總結

要學會生產對象,也要學會管理對象、回收對象。


歡迎訪問南瓜慢說 www.pkslow.com獲取更多精彩文章!

歡迎關注微信公衆號<南瓜慢說>,將持續爲你更新...

多讀書,多分享;多寫做,多整理。

相關文章
相關標籤/搜索