Java方法的應用

來源:互聯網
上載者:User

標籤:apple   png   serve   style   cond   ice   ide   使用數組   實參   

一、方法的定義

     方法是類的組成部分,是解決一類問題的步驟的有序組合,包含於類或對象中,在程式中被建立,在其他地方被引用

方法的文法:

     修飾符 傳回值類型 方法名(參數類型 參數名.....){ ... 方法體 ... return 傳回值; }

根據方法是否帶參、是否帶傳回值,可將方法分為四類:

  • 無參無傳回值方法
  • 無參帶傳回值方法
  • 帶參無傳回值方法
  • 帶參帶傳回值方法
package com.yyx.pratice;public class JavaPratice {    public static void main(String[] args) {        noParamValue();        String aStr = "有參數", bStr = "無傳回值";        noValue(aStr, bStr);        System.out.println(noParam());        System.out.println(ParamValue("有參數有傳回值"));    }    public static void noParamValue() {        System.out.println("無參數無傳回值");    }    /**     * 有參數無傳回值     *      * @param aStr     * @param bStr     */    public static void noValue(String aStr, String bStr) {        System.out.println(aStr + bStr);    }    /**     * 無參數有傳回值     *      * @return     */    public static String noParam() {        String str = "無參數有傳回值";        return str;    }    /**     * 有參數,有傳回值     *      * @param str     * @return     */    public static String ParamValue(String str) {        return str;    }}

二、方法的重載

     方法重載的特點:

  •            同一個類中
  •            方法名必須相同
  •            方法的參數列表不同(參數的個數不同或者參數類型不同)
  •           方法的重載與方法的傳回值類型沒有關係  
package com.yyx.pratice;public class JavaPratice {    public static void main(String[] args) {        int a = 1, b = 2, c = 3;        double m = 2.1, n = 3.2;        addNumber(a, b);        addNumber(m, n);        System.out.println(addNumber(a, b, c));    }    public static void addNumber(int a, int b) {        System.out.println(a + b);    }    public static void addNumber(double a, double b) {        System.out.println(a + b);    }    public static int addNumber(int a, int b, int c) {        return a + b + c;    }}

三、方法的重寫   

      在子類中可以根據需要對從基類繼承來的方法進行重寫

重寫的規則:

  • 要求子類方法的“傳回值類型 方法名 (參數列表)”與父類的方法一樣
  • 子類方法的修飾符不能小於父類方法的修飾符
  • 若父類方法拋異常,那麼子類方法拋的異常類型不能大於父類的。
  • 子父類的方法必須同為static或同為非static的
package com.yyx.pratice;public class JavaPratice {    public static void main(String[] args) {        TestExtend testExtend = new TestExtend();        testExtend.addNumber(5, 6);        testExtend.printWord("測試重寫方法");    }}class TestInheritance {    public void printWord(String str) {        System.out.println(str);    }}class TestExtend extends TestInheritance implements TestImplements {    @Override    public void printWord(String str) {        System.out.println("子類重新加一句");    }    @Override    public void addNumber(int m, int n) {        System.out.println(m + n);    }}interface TestImplements {    void addNumber(int m, int n);}

四、方法的參數

   1.形參和實參

      形參:方法聲明時,方法小括弧內的參數;實參:調用方法時,實際傳入的參數的值

參數傳遞機制:

  • 形參是基礎資料型別 (Elementary Data Type)的:將實參的值傳遞給形參的基礎資料型別 (Elementary Data Type)的變數,即傳遞值
  • 形參是引用資料類型的:將實參的參考型別變數的值(對應的堆空間的對象實體的首地址值)傳遞給形參的參考型別變數,即傳遞地址
package com.yyx.pratice;public class TestValue {    public static void main(String argv[]) {        TestValue t = new TestValue();        t.first();    }    public void first() {        int i = 5;        Value v = new Value();        v.i = 25;        second(v, i);        System.out.println(v.i);    }    public void second(Value v, int i) {        i = 0;        v.i = 20;        Value val = new Value();        v = val;        System.out.println(v.i + " " + i);    }}class Value {    int i = 15;}

  2.可變參數

      可變個數的形參的方法:

  • 1.格式:對於方法的形參: 資料類型 ... 形參名
  • 2.可變個數的形參的方法與同名的方法之間構成重載
  • 3.可變個數的形參在調用時,個數從0開始,到無窮多個都可以
  • 4.使用可變多個形參的方法與方法的形參使用數組是一致的
  • 5.若方法中存在可變個數的形參,那麼一定要聲明在方法形參的最後
  • 6.在一個方法中,最多聲明一個可變個數的形參
public class TestArgs {    public static void main(String[] args) {        TestArgs t = new TestArgs();        // t.sayHello(new String[]{"hello China","hello BeiJing"});        t.sayHello("hello China", "hello BeiJing");        t.sayHello(5, "hello China", "hello BeiJing");    }    // 可變個數的形參的方法    public void sayHello(String... args) {        for (int i = 0; i < args.length; i++) {            System.out.println(args[i] + "$");        }    }    public void sayHello(int i, String... args) {        System.out.println(i);        for (int j = 0; j < args.length; j++) {            System.out.println(args[j] + "$");        }    }    /**     * 使用可變多個形參的方法與方法的形參使用數組是一致的     */    // public void sayHello(String[] args) {    // for (int i = 0; i < args.length; i++) {    // System.out.println(args[i]);    // }    // }}

 

Java方法的應用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.