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

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # Java 中的抽象方法 > 原文: [https://beginnersbook.com/2014/01/abstract-method-with-examples-in-java/](https://beginnersbook.com/2014/01/abstract-method-with-examples-in-java/) 沒有主體的方法(沒有實現)被稱為抽象方法。方法必須始終在抽象類中聲明,或者換句話說,如果類具有抽象方法,則應該將其聲明為抽象方法。在上一個教程中我們討論了抽象類,如果你還沒有檢查過它,請在這里閱讀它: [Java 中的抽象類](https://beginnersbook.com/2013/05/java-abstract-class-method/),在閱讀本指南之前。 這是一個抽象方法在 java 中的外觀: ```java public abstract int myMethod(int n1, int n2); ``` 如你所見,這沒有身體。 ## 抽象方法規則 1. 抽象方法沒有正文,它們只有如上所示的方法簽名。 2. 如果一個類有一個抽象方法,它應該被聲明為`abstract`,反之亦然,這意味著一個抽象類不需要一個抽象方法是強制性的。 3. 如果常規類擴展了一個抽象類,那么該類必須實現抽象父類的所有抽象方法,或者它也必須被聲明為`abstract`。 ## 示例 1:抽象類中的抽象方法 ```java //abstract class abstract class Sum{ /* These two are abstract methods, the child class * must implement these methods */ public abstract int sumOfTwo(int n1, int n2); public abstract int sumOfThree(int n1, int n2, int n3); //Regular method public void disp(){ System.out.println("Method of class Sum"); } } //Regular class extends abstract class class Demo extends Sum{ /* If I don't provide the implementation of these two methods, the * program will throw compilation error. */ public int sumOfTwo(int num1, int num2){ return num1+num2; } public int sumOfThree(int num1, int num2, int num3){ return num1+num2+num3; } public static void main(String args[]){ Sum obj = new Demo(); System.out.println(obj.sumOfTwo(3, 7)); System.out.println(obj.sumOfThree(4, 3, 19)); obj.disp(); } } ``` 輸出: ```java 10 26 Method of class Sum ``` ## 例 2:接口中的抽象方法 默認情況下,[接口](https://beginnersbook.com/2013/05/java-interface/)的所有方法都是公共抽象的。您不能在接口中使用具體的(常規方法和正文)方法。 ```java //Interface interface Multiply{ //abstract methods public abstract int multiplyTwo(int n1, int n2); /* We need not to mention public and abstract in interface * as all the methods in interface are * public and abstract by default so the compiler will * treat this as * public abstract multiplyThree(int n1, int n2, int n3); */ int multiplyThree(int n1, int n2, int n3); /* Regular (or concrete) methods are not allowed in an interface * so if I uncomment this method, you will get compilation error * public void disp(){ * System.out.println("I will give error if u uncomment me"); * } */ } class Demo implements Multiply{ public int multiplyTwo(int num1, int num2){ return num1*num2; } public int multiplyThree(int num1, int num2, int num3){ return num1*num2*num3; } public static void main(String args[]){ Multiply obj = new Demo(); System.out.println(obj.multiplyTwo(3, 7)); System.out.println(obj.multiplyThree(1, 9, 0)); } } ``` 輸出: ```java 21 0 ``` **參考:** [抽象方法 javadoc](https://docs.oracle.com/javase/tutorial/java/IandI/abstract.html)
                  <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>

                              哎呀哎呀视频在线观看