<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 構造器 > 原文: [https://javabeginnerstutorial.com/core-java-tutorial/constructors-in-java/](https://javabeginnerstutorial.com/core-java-tutorial/constructors-in-java/) Java 中的構造器可以視為類中的方法。 但是構造器和方法之間有很大的區別。 可以根據目的,語法和調用來定義這些差異。 ## Java 構造器的目的(Vs 方法) 構造器只有一個目的,**創建一個類**的實例。 該實例包括內存分配和成員初始化(*可選*)。 相比之下,不能使用方法創建類的實例。 ## 構造器的語法(Vs 方法) ```java /* * Here Class name is ConstructorExample, So constructor name needs to be the same. */ public class ConstructorExample { /* * As below signature has the name as Class name and it doesn't contain any * return value so it will be treated as Constructor of the class */ public ConstructorExample() { System.out.println("Inside Constructor"); } /* * Below method will be invoked only when it is invoked implicitly. * Method has return type along with Non Access Modifier */ static void method() { System.out.println("This is in method"); } } ``` 構造器的語法不同于下面描述的方法。 * 構造器不能具有非訪問修飾符,而方法可以。 * 構造器不能具有返回類型(*包括`void`*),而方法需要它。 * 構造器名稱必須與類名稱相同,而方法不受限制。 * 根據 Java 命名約定,方法名稱應為駝峰式,而構造方法名稱應以大寫字母開頭。 > 方法可以具有與類名稱相同的名稱。 ## 構造器的調用(Vs 方法) 構造器和方法的調用方式有所不同。 構造器不能顯式調用,在生成類的實例時將隱式調用構造器(*使用`new`關鍵字*) ### 構造器調用示例 ```java /* * Here Class name is ConstructorExample, So constructor name needs to be the same. */ public class ConstructorExample { /* * As below signature has the name as Class name and it doesn't contain any * return value so it will be treated as Constructor of the class */ public ConstructorExample() { System.out.println("Inside Constructor"); } public static void main(String args[]) { ConstructorExample cls = new ConstructorExample(); } } //Output will be //Inside Constructor ``` ### 方法調用示例 ```java /* * Here Class name is ConstructorExample, So constructor name needs to be the same. */ public class ConstructorExample { /* * As below signature has the name as Class name and it doesn't contain any * return value so it will be treated as Constructor of the class */ public ConstructorExample() { System.out.println("Inside Constructor"); } /* * Below method will be invoked only when it is invoked implicitly. */ void method() { System.out.println("This is in method"); } public static void main(String args[]) { ConstructorExample cls = new ConstructorExample(); /* * Now method will be called explicitly as below. It will execute the * code within method. */ cls.method(); } } //The output would be Inside Constructor This is in method ``` 類中的構造器必須與給定的類具有相同的名稱。 構造器的語法*不包含返回類型*,因為構造器從不返回值。 構造器還可以包括各種類型的參數。 使用`new`運算符調用構造器時,類型必須與構造器定義中指定的類型匹配。 如果未提供顯式構造器,則 Java 提供一個*默認構造器*,其中**不帶參數**并且不執行任何特殊操作或初始化。 隱式默認構造器執行的唯一操作是使用`super()`調用來調用超類構造器。 ## Java 構造器規則 * 構造器**不能**具有**返回類型**。 * 構造器**必須**與類具有相同的名稱。 * 構造器**無法標記為靜態** * 構造器**不能標記為抽象** * 構造器**不能**被**覆蓋**。 * 構造器**不能**是最終的。 > 如果類定義了顯式構造器,則它不再具有默認的構造器來設置對象的狀態。 如果此類需要默認構造器(不帶參數的構造器),則必須提供其實現。 > > 如果在這種情況下未提供顯式的默認構造器,則任何調用默認構造器的嘗試都將是編譯時錯誤。 ## 構造器重載: 像方法一樣,構造器也可以重載。 由于類中的所有構造器都具有與該類相同的名稱,因此它們的簽名通過其參數列表來區分。 可以使用`this()`構造在類中實現構造器的本地鏈接。 構造器中的`this()`調用使用同一類中的相應參數列表來調用其他構造器。 Java 要求任何`this()`調用都必須在構造器中作為**第一條語句**發生。 ## 構造器鏈接: **每個構造器中都包含一個隱式`super()`調用,該構造器不包含`this()`或顯式`super()`調用作為第一個調用語句。** `super()`語句用于調用超類的構造器。 隱式`super()`可以由顯式`super()`代替。 超級語句必須是構造器的第一條語句。 顯式超類允許將參數值傳遞給其超類的構造器,并且必須具有匹配的參數類型。 子類的構造器中的`super()`調用將基于調用的簽名,導致超類中相關構造器的調用。 這稱為構造器鏈接。 `super()`或`this()`構造:如果在構造器中使用,則它必須作為構造器中的第一條語句出現,并且只能在構造器聲明中使用。 這意味著`this()`和`super()`調用不能同時出現在同一構造器中。 就像`this()`構造導致同一類中的構造器鏈接一樣, `super()`構造也導致了子類構造器與超類構造器的鏈接。 如果構造器既沒有`this()`也沒有`super()`構造作為其第一條語句,則調用超類默認構造器的`super()`被隱式插入。 > 如果一個類僅定義非默認構造器,則其子類將不包含隱式`super()`調用。 這將被標記為編譯時錯誤。 > > 然后,子類必須使用帶有正確參數的`super()`構造與超類的適當構造器進行匹配,來顯式調用超類構造器。 ### 備忘單 * 創建新的**對象**時,調用構造器。 * 構造器也可以*重載*,但不能將*覆蓋*。 * 每個類至少有一個構造器。 如果用戶不提供任何內容,則 JVM 將*提供*默認的*無參數構造器*。 * *抽象類*也**具有**構造器。 * 構造器**必須與該類具有相同名稱**。 * 構造器不能具有返回類型。 * 如果與類同名的方法具有返回類型,則將其視為普通成員方法,而不是構造器。 * 構造器可以具有**任何訪問修飾符(全部)**。 * 默認構造器是一個無參構造器,它調用超類的無參構造器。 如果超類沒有任何無參數構造器,則它將拋出運行時異常。 * 在類具有默認構造器的情況下,其超類需要具有無參數構造器。 * *構造器的第一個語句*可以是`this`或`super`,但不能同時使用。 * 如果編碼器未編寫任何`this()`或`super()`調用,則編譯器將添加`super()`調用。 * `super()`用于從超類調用構造器。 * `this()`用于從同一類調用構造器。 * *實例成員*僅在超類構造器運行后才能訪問。 * 接口沒有構造器。 * *構造器*是未繼承的。 因此,無法將*覆蓋*。 * 構造器不能直接調用。 當創建新對象或由*其他構造器*調用新對象時,將被調用(**隱式**)。 <https://www.youtube.com/embed/l0b2qWuty2E?start=1&amp;feature=oembed>
                  <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>

                              哎呀哎呀视频在线观看