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

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # Java 重載 > 原文: [https://javabeginnerstutorial.com/core-java-tutorial/overloading/](https://javabeginnerstutorial.com/core-java-tutorial/overloading/) 重載方法為您提供了一個選項,可以在類中使用與*相同的方法名稱*,但具有*不同的參數*。 ## Java 方法規則中的重載 有一些與重載方法相關的規則。 #### 重載方法 * 必須更改參數列表 * 可以更改返回類型 * 可以更改訪問修飾符(更廣泛) * 可以聲明一個新的或更廣泛的受檢異常 ###### 方法可以在類或子類中重載。 ## 重載方法示例 ```java //Overloaded method with one argument public void add(int input1, int input2) { System.out.println("In method with two argument"); } //Overloaded method with one argument public void add(int input1) { System.out.println("In method with one argument"); } ``` ## 調用重載方法 在幾種可用的重載方法中,調用的方法基于參數。 ```java add(3,4); add(5); ``` 第一個調用將執行第一個方法,第二個調用將執行第二個方法。 ###### 引用類型而不是對象決定調用哪個重載方法,和覆蓋方法相反。 感謝 Manoj 指出拼寫錯誤。 ## 方法重載備忘單 * 使用相同的方法名稱但使用*不同的參數*稱為重載。 * **構造器**也可以重載 * 重載的方法必須設置*不同的*參數。 * 重載的方法**可能**具有不同的返回類型。 * 重載的方法**可能**具有不同的訪問修飾符。 * 重載的方法**可能**拋出不同的異常,**更寬或更窄的**沒有限制。 * **超類**中的方法也可以在子類中重載。 * 多態適用于覆蓋和重載。 * 基于**引用類型**,確定在**編譯時間**上確定將調用哪個重載方法。 方法重載示例 ```java package com.overloading; /* * Here we will learn how to overload a method in the same class. * Note: Which overloaded method is to invoke is depends on the argument passed to method. */ public class MethodOverloading { public static void main(String args[]) { //Creating object of the class MethodOverloading MethodOverloading cls = new MethodOverloading(); System.out.println("calling overloaded version with String parameter"); cls.method("Hello"); System.out.println("calling overloaded version with Int parameter"); cls.method(3); /* * Here same method name has been used, But with different argument. * Which method is to invoke is decided at the compile time only */ } /* * Overloaded version of the method taking string parameter * name of the method are same only argument are different. */ void method(String str) { System.out.println("Value of the String is :"+str); } /* * Overloaded version taking Integer parameter */ void method(int i) { System.out.println("Value of the Int is :"+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>

                              哎呀哎呀视频在线观看