<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/java-overriding/](https://javabeginnerstutorial.com/core-java-tutorial/java-overriding/) 從其**超類**繼承該方法的類可以選擇**覆蓋**它。 覆蓋的好處是可以定義特定于特定類的行為的能力。 對于具體的子類,如果在層次結構中沒有其他超類實現抽象類中定義的所有方法,則將其強制實現。 覆蓋有時稱為*運行時綁定*。 這意味著將調用哪個覆蓋方法將由引用類型而不是實例類型確定。 * * * ## 方法覆蓋示例 ```java public class ParentClass { public void show() { System.out.println("Show method of Super class"); } } public class SubClass extends ParentClass { //below method is overriding the ParentClass version of show method public void show() { System.out.println("Show method of Sub class"); } } ``` * * * ## 方法覆蓋規則 * 覆蓋方法*不能*具有比被覆蓋的方法更多的*限制性訪問修飾符*,但可以更少。 * 參數列表必須與覆蓋的方法完全匹配,如果不匹配,則很可能是您正在重載該方法。 * 返回類型必須與在超類中的覆蓋方法中聲明的返回類型相同或為該子類型的子類型。 * 覆蓋方法*可以*拋出任何*非受檢異常(運行時)*,但是它可以拋出比被覆蓋方法聲明的范圍更廣或更新的受檢異常,但不能拋出更少或狹窄的異常檢查。 * *不能覆蓋最終的方法*。 * *靜態*方法*無法覆蓋*。 靜態方法看起來可以覆蓋,但它是隱藏的。 * 如果*方法無法繼承,則無法覆蓋*。 ## 從超類調用被覆蓋方法 如果要在執行子類方法之前調用超類的覆蓋方法,該怎么辦。 您可以使用`SUPER`關鍵字。 ```java public class SubClass extends superclass { void method() { super.method(); System.out.println("In Sub Class"); } public static void main(String[] args) { SubClass obj = new SubClass(); obj.method(); } } class superclass { void method() { System.out.println("In Super Class"); } } ``` 輸出量 ```java In Super Class In Sub Class ``` ### 靜態方法不能被覆蓋 靜態方法不能被覆蓋。 看起來好像它已被覆蓋,但事實并非如此。 靜態方法可以隱藏。 ```java public class SubClass extends superclass { static void method() { // super.method(); // Super keyword will not work here. As it is not overriden method System.out.println("In Sub Class"); } @SuppressWarnings("static-access") // The static method method() from the type SubClass should be accessed in a static way public static void main(String[] args) { SubClass obj = new SubClass(); obj.method(); ?? ???? SubClass.method();// It is same as above. Same method will be invoked } } class superclass { static void method() { System.out.println("In Super Class"); } } ``` 這里`super`關鍵字不能用于調用超類方法。 因為它沒有被超類的方法覆蓋。 ## 備忘單 * *不能覆蓋構造器*。 * 覆蓋方法必須具有相同的參數集。 * 覆蓋的方法必須具有相同的返回類型。 這些返回類型也可以是子類(**協變返回**)。 * 覆蓋的方法**無法**具有更嚴格的訪問修飾符。 * 覆蓋的方法**無法**拋出*新的或更廣泛的*異常(**受檢**)。 看下面的示例 * 覆蓋的方法**可以引發**任何*非受檢異常*。 * **最終**方法**無法覆蓋**。 * **私有方法**沒有繼承到子類,因此*不能*在子類中覆蓋*。 * 多態適用于覆蓋。 * **對象類型**確定將調用哪個覆蓋方法,并由*運行時*確定。 ### 方法覆蓋異常示例 ```java package com.example.simple; public class Overridding_Class extends base_class { @Override void method() throws exception_3 { // NO PROBLEM, It is not a broader Exception. } void method1() throws exception_1 { // It will give COMPILATION ERROR as it is throwing Broader Exception } } class base_class { void method() throws exception_2 { } void method1() throws exception_2 { } } class excepion_1 extends Exception { } class exception_2 extends excepion_1 { } class exception_3 extends exception_2 { } ``` 方法覆蓋示例 ```java package com.override; public class MethodOverrideRule { public static void main(String args[]) { // Here reference type and Object type is same MethodOverrideRule scls = new MethodOverrideRule(); OverrideSubclass subcls = new OverrideSubclass(); // Here reference type is of Super class and Object is of child class MethodOverrideRule subOcls = new OverrideSubclass(); // This will invoke method from Super class scls.method(); // This will onvoke method form sub class subcls.method(); /* * Here overriding will work. Even reference type is of Super class still object type if of Subclass. * Hence Subclass version of method will get invoked. */ subOcls.method(); /* * Which overridden method is to be executed depends on the actual Object type at run time. */ } void method(){ System.out.println("Overriding method without argument in Super"); } int method(String str) { System.out.println("Overriding method with int argument in Super"); return 9; } } class OverrideSubclass extends MethodOverrideRule{ /* * Here we are overriding the method from super class. * @Override annotation is used to confirm the same. It makes sure the all override rules get followed * */ @Override void method(){ System.out.println("Overriding method without argument in subclass"); } @Override int method(String str) { System.out.println("Overriding method with int argument in subclass"); return 10; } } ```
                  <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>

                              哎呀哎呀视频在线观看