<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>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # 用 Java 創建對象的不同方法 > 原文: [https://javabeginnerstutorial.com/core-java-tutorial/different-ways-to-create-an-object-in-java/](https://javabeginnerstutorial.com/core-java-tutorial/different-ways-to-create-an-object-in-java/) 您必須知道使用`new`關鍵字創建類的對象,但這不是創建對象的唯一方法。 還有其他幾種創建類對象的方法: * 使用`new`關鍵字 * 使用新實例(反射) * 使用克隆 * 使用反序列化 * 使用`ClassLoader` * …不知道了 ?? ## 使用`new`關鍵字 使用`new`關鍵字是創建對象的最基本方法。 **`new`關鍵字可用于創建類的對象。** ```java public class ObjectCreationExample { public static void main(String[] args) { // Here we are creating Object of JBT using new keyword JBT obj = new JBT(); } } class JBT{ String Owner; } ``` ## 使用`newInstance`(反射) 您是否曾經嘗試使用 Java 中的 JDBC 驅動程序連接到數據庫? 如果您的回答是肯定的,那么您必須已經看到“`Class.forName`”。 我們還可以使用它來創建類的對象。 `Class.forName`實際上是在 Java 中加載該類,但未創建任何對象。 要創建對象,必須使用`Class`類的`newInstance`方法。 ```java /* * Here we will learn to create Object of a class without using new Operator. * But newInstance method of Class class. */ class CreateObject { public static void main(String[] args) { try { Class cls = Class.forName("JBTClass"); JBTClass obj = (JBTClass) cls.newInstance(); JBTClass obj1 = (JBTClass) cls.newInstance(); System.out.println(obj); System.out.println(obj1); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } } class JBTClass { static int j = 10; JBTClass() { i = j++; } int i; @Override public String toString() { return "Value of i :" + i; } } ``` > 如果要以這種方式創建對象,則類需要具有公開的默認構造器。 ## 使用`Clone` 我們還可以使用`Clone()`方法創建現有對象的*副本*。 ```java /* * Here we will learn to create an Object of a class without using new Operator. * For this purpose we will use Clone Interface */ class CreateObjectWithClone { public static void main(String[] args) { JBTClassClone obj1 = new JBTClassClone(); System.out.println(obj1); try { JBTClassClone obj2 = (JBTClassClone) obj1.clone(); System.out.println(obj2); } catch (CloneNotSupportedException e) { e.printStackTrace(); } } } class JBTClassClone implements Cloneable { @Override protected Object clone() throws CloneNotSupportedException { return super.clone(); } int i; static int j = 10; JBTClassClone() { i = j++; } @Override public String toString() { return "Value of i :" + i; } } ``` **關于克隆**的其他說明 * 在這里,我們正在創建現有對象而不是任何新對象的克隆。 * 克隆方法在`Object`類中聲明為受保護的。 因此,只能在子類或同一包中對其進行訪問。 這就是為什么它在類上被覆蓋的原因。 * 類需要實現`Cloneable`接口,否則它將引發`CloneNotSupportedException`。 ## 使用對象反序列化 對象反序列化也可以用于創建對象。 它產生與序列化對象相反的操作。 ## 使用`ClassLoader` 我們也可以使用`ClassLoader`創建類的對象。 這種方式與`Class.forName`選項相同。 ```java /* * Here we will learn to Create an Object using Class Loader */ public class CreateObjectWithClassLoader { public static void main(String[] args) { JBTClassLoader obj = null; try { obj = (JBTClassLoader) new CreateObjectWithClassLoader().getClass() .getClassLoader().loadClass("JBTClassLoader").newInstance(); // Fully qualified classname should be used. } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } System.out.println(obj); } } class JBTClassLoader { static int j = 10; JBTClassLoader() { i = j++; } int i; @Override public String toString() { return "Value of i :" + i; } } ``` ###### 下一篇文章
                  <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>

                              哎呀哎呀视频在线观看