<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                ### JAVA對象實例化的方法 ##### New對象實例 ``` // 直接new對象實例 Productor productor = new Productor(); ``` _注意:任何類都有構造方法,但是new指令只能創建非抽象類的對象;構造方法不能是靜態的,因為靜態方法不能使用this,而構造方法中可以使用_ ##### 反射機制 Java反射機制是在運行狀態中,對于任意一個類,都能夠知道這個類的所有屬性和方法;對于任意一個對象,都能夠調用它的任意方法和屬性;這種動態獲取信息以及動態調用對象方法的功能稱為Java語言的反射機制。 反射機制創建對象分為兩種,一種是Class類的newInstance\(\),另一種是java.lang.reflect.Constructor類的newInstance\(\)。 兩者區別在于: * Class.newInstance\(\) 只能夠調用無參的構造函數,即默認的構造函數; * Constructor.newInstance\(\) 可以根據傳入的參數,調用任意構造構造函數。 事實上Class的newInstance方法內部調用Constructor的newInstance方法。 反射機制創建對象,使用的是類加載機制,newInstance\(\)的特點是在調用時才創建對象,通過類加載機制Class.forName\("xxx"\).newInstance\(\)創建對象,xxx可以從配置文件當中獲取實際的值,這樣達到了解耦的目的,也是Spring依賴注入的原理 ``` // 1.通過反射調用Java.lang.Class Productor productor = (Productor) Class.forName("com.quancheng.Productor").newInstance(); Productor productor = Productor.class.newInstance(); // 2.java.lang.reflect.Constructor類的newInstance()實例方法 Constructor<Productor> constructor = Productor.class.getConstructor(); Productor productor = constructor.newInstance(); ``` ##### clone\(\)方法 clone方法分為淺拷貝和深拷貝 * 淺拷貝:被復制對象的所有變量都含有與原來的對象相同的值,而所有的對其他對象的引用仍然指向原來的對象。 * 深拷貝:不僅要復制對象的所有非引用成員變量值,還要為引用類型的成員變量創建新的實例,并且初始化為形式參數實例值。 淺拷貝的效果就是,對引用對象的操作,會影響到所有引用了該對象的對象 ``` public class Object { // clone方法定義 protected native Object clone() throws CloneNotSupportedException; } ``` _注意:_ 1. _Cloneable只是一個標志,想要使用super.clone\(\),則需要實現Cloneable接口,否則就會拋出CloneNotSupportedException異常;_ 2. _clone\(\)實現的是淺復制,在重載clone方法的時候,需要轉為深復制。即屬性存在對象引用的時候,需要對引用屬性再進行clone\(\)復制,直到沒有對象引用;_ ``` public class Appliction { public static void main(String[] args) throws CloneNotSupportedException { User user = new User(); user.setName("Tom"); user.setAge(20); user.setWeight(120); System.err.println(user); User u = (User) user.clone(); System.err.println(u); } } class User implements Cloneable { private String name; private int age; private Integer weight; @Override protected Object clone() throws CloneNotSupportedException { return super.clone(); } ...... } ``` ##### 反序列化 序列化對象就是對象此刻在內存中的狀態轉成的字節碼。通過實現Serializable接口進行序列化。同Cloneable一樣,Serializable也是一個空接口,作為一個標志使用。通過ObjectStream的writeObject\(\)方法和readObject\(\)方法來序列化和反序列化 還有一個Externalizable接口同樣可以實現序列化,Externalizable繼承了Serializable,同時增加了writeExternal\(\)和readExternal\(\)兩個方法,可以指定序列化哪些屬性,對于需要隱藏的屬性,在前面加上transient就可以。 _注意:序列化和反序列化是深復制,static、transient 后的變量無法序列化_ ##### Unsafe 使用Unsafe"黑科技"實例化對象 ``` public class Appliction { private static Unsafe U; public static void main(String[] args) throws InstantiationException { User user = (User) U.allocateInstance(User.class); user.setName("Tom"); System.err.println(user.getName()); } static { try { Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe"); theUnsafe.setAccessible(true); U = (Unsafe) theUnsafe.get("null"); } catch (NoSuchFieldException | IllegalAccessException e) { e.printStackTrace(); } } } class User implements Cloneable { private String name; private int age; private Integer weight; @Override protected Object clone() throws CloneNotSupportedException { return super.clone(); } ...... } ``` _注意:使用Unsafe方法實例化對象不會調用對象的構造方法_
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看