方法(Function)

方法(Function):完成特定功能的代碼塊

  注意:在不少語言裏面有函數的定義,而在Java中函數被稱爲方法。java

     方法格式:
      修飾符 返回值類型 方法名(參數類型 參數名1,參數類型 參數名2...) {
      方法體語句;
      return 返回值;
      }
  詳細解釋:
    修飾符:目前就用 public static。後面咱們再詳細的講解其餘的修飾符。
    返回值類型:就是功能結果的數據類型。
    方法名:符合命名規則便可。方便咱們的調用。
    參數:
      實際參數:就是實際參與運算的。
      形式參數;就是方法定義上的,用於接收實際參數的。
      參數類型:就是參數的數據類型
      參數名:就是變量名
      方法體語句:就是完成功能的代碼。
      return:結束方法的。
      返回值:就是功能的結果,由return帶給調用者。

  要想寫好一個方法,就必須明確兩個東西:
    A:返回值類型
      結果的數據類型
    B:參數列表
      你要傳遞幾個參數,以及每一個參數的數據類型

  需求:求兩個數據之和的案例

  方法的執行特色:
    不調用,不執行。

  如何調用呢?(有明確返回值的調用)
    A:單獨調用,通常來講沒有意義,因此不推薦。
    B:輸出調用,可是不夠好。由於咱們可能須要針對結果進行進一步的操做。
    C:賦值調用,推薦方案。jvm

  Demo1:函數

class FunctionDemo1 {
    public static void main(String[] args) {
        int x = 10;
        int y = 20;
        
        //方式1:單獨調用
        //sum(x,y);
    
        //方式2:輸出調用
        //System.out.println(sum(x,y));
        //System.out.println(30);
    
        //方式3:賦值調用
        int result = sum(x,y);
        //result在這裏能夠進行操做
        System.out.println(result);
    }
    
    /*
        需求:求兩個數據之和的案例
        
        兩個明確:
            返回值類型:int
            參數列表:2個,都是int類型。
    */
    public static int sum(int a,int b) {
            //如何實現呢?
            //int c = a + b;
            //return c;
            
            //c就是a+b,因此,我能夠直接返回a+b
            return a + b;
    }
    
}

 

Demo2:測試

  需求:鍵盤錄入兩個數據,返回兩個數中的較大值優化

import java.util.Scanner;

class FunctionDemo2 {
    public static void main(String[] args) {
        //建立鍵盤錄入對象
        Scanner sc = new Scanner(System.in);
        
        System.out.println("請輸入第一個數據:");
        int a = sc.nextInt();
        
        System.out.println("請輸入第二個數據:");
        int b = sc.nextInt();
        
        int result = getMax(a,b);
        System.out.println("較大值是:"+result);
    }
    
    /*
        需求:兩個數中的較大值
        兩個明確:
            返回值類型:int
            參數列表:int a,int b            
    */
    public static int getMax(int a,int b) {
        //if語句
        /*
        if(a > b) {
            //System.out.println(a);
            return a;
        }else {
            //System.out.println(b);
            return b;
        }
        */
        
        //用三元改進
        //int c = ((a > b)? a: b);
        //return c;
        
        //因爲c就是後面的式子
        return ((a>b)? a : b);
    }
}

 

Demo3:(語句優化)spa

  需求:code

    鍵盤錄入兩個數據,比較兩個數是否相等對象


  分析:
    比較兩個數是否相等結果是一個boolean類型。blog

import java.util.Scanner;

class FunctionDemo3 {
    public static void main(String[] args) {
        //建立鍵盤錄入對象
        Scanner sc = new Scanner(System.in);
        
        System.out.println("請輸入第一個數據:");
        int a = sc.nextInt();
        
        System.out.println("請輸入第二個數據:");
        int b = sc.nextInt();
        
        boolean flag = compare(a,b);
        System.out.println(flag);
    }
    
    /*
        需求:比較兩個數是否相等
        兩個明確:
            返回值類型:boolean
            參數列表:int a,int b
    */
    public static boolean compare(int a,int b) {
        //if語句的格式2實現
        /*
        if(a == b) {
            return true;
        }else {
            return false;
        }
        */
        
        //三元改進
        //boolean flag = ((a==b)? true: false);
        //return flag;
        
        //繼續改進
        //return ((a==b)? true: false);
        
        //最終版
        return a == b;
    }
}

 

Demo4:(加強代碼的可閱讀性)get

  需求:鍵盤錄入三個數據,返回三個數中的最大值

import java.util.Scanner;

class FunctionTest3 {
    public static void main(String[] args) {
        //建立鍵盤錄入對象
        Scanner sc = new Scanner(System.in);
        
        System.out.println("請輸入第一個數據:");
        int a = sc.nextInt();
        
        System.out.println("請輸入第二個數據:");
        int b = sc.nextInt();
        
        System.out.println("請輸入第三個數據:");
        int c = sc.nextInt();
        
        int max = getMax(a,b,c);
        System.out.println("三個數據中的最大值是:"+max);
    }
    
    /*
        需求;返回三個數中的最大值
        
        兩個明確:
            返回值類型:int
            參數列表:int a,int b,int c
    */
    public static int getMax(int a,int b,int c) {
        //if嵌套
        /*
        if(a > b) {
            if(a > c) {
                return a;
            }else {
                return c;
            }
        }else {
            if(b > c) {
                return b;
            }else {
                return c;
            }
        }
        */
        
        //用三元改
        /*
        if(a > b) {
            return (a>c? a: c);
        }else {
            return (b>c? b: c);
        }
        */
        
        //繼續改進
        //return (a>b)? (a>c? a: c): (b>c? b: c);
        //不建議,寫代碼必定要注意閱讀性強
        int temp = ((a>b)? a: b);
        int max = ((temp>c)? temp: c);
        return max;
    }
}

 

  方法的注意事項:
    A:方法不調用不執行
    B:方法與方法是平級關係,不能嵌套定義
    C:方法定義的時候參數之間用逗號隔開
    D:方法調用的時候不用在傳遞數據類型
    E:若是方法有明確的返回值,必定要有return帶回一個值

class FunctionDemo5 {
    public static void main(String[] args) {
        /*
        錯誤的
        public static int sum(int a,int b){
            return a + b;
        }
        */
        
        //sum(10,20);
        
        //int x = 10;
        //int y = 20;
        //錯誤
        //sum(int x,int y);
    }
    
    public static int sum(int a,int b){
        return a + b;
    }
}

 

void類型方法調用:
  需求:在控制檯輸出以下的形狀
    *****
    *****
    *****
    *****

  void類型返回值的方法調用:
    單獨調用
    輸出調用(錯誤,此處不容許使用 '空' 類型)
    賦值調用(錯誤,非法的表達式)

class FunctionDemo6 {
    public static void main(String[] args) {
        //for循環嵌套輸出圖形
        for(int x=0; x<4; x++) {
            for(int y=0; y<5; y++) {
                System.out.print("*");
            }
            System.out.println();
        }
        System.out.println("--------------");
        
        //需求:我要在控制檯輸出一個6行7列的星形圖形
        for(int x=0; x<6; x++) {
            for(int y=0; y<7; y++) {
                System.out.print("*");
            }
            System.out.println();
        }
        System.out.println("--------------");
        
        //若是須要繼續改變,咱們就應該考慮使用方法改進。
        //單獨調用
        printXing(3,4);
        System.out.println("--------------");
        printXing(6,7);
        System.out.println("--------------");
        printXing(8,9);
        
        //輸出調用
        //此處不容許使用 '空' 類型
        //System.out.println(printXing(3,4));
        
        //賦值調用
        //非法的表達式
        //void v = printXing(3,4);
    }
    
    /*
        寫一個什麼樣子的方法呢?寫一個m行n列的代碼
        
        兩個明確:
            返回值類型:這個時候沒有明確的返回值,不寫東西還不行,因此,這裏記住是void
            參數列表:int m,int n
    */
    public static void printXing(int m,int n) {
        for(int x=0; x<m; x++) {
            for(int y=0; y<n; y++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }
}

   

  需求:鍵盤錄入行數和列數,輸出對應的星形

import java.util.Scanner;

class FunctionDemo7 {
    public static void main(String[] args) {
        //建立鍵盤錄入對象
        Scanner sc = new Scanner(System.in);
        
        System.out.println("請輸入行數:");
        int m = sc.nextInt();
        
        System.out.println("請輸入列數:");
        int n = sc.nextInt();
        
        //void類型的方法調用
        printXing(m,n);
    }
    
    /*
        輸出星形
        
        兩個明確:
            返回值類型:void
            參數列表:int m,int n
    */
    public static void printXing(int m,int n) {
        for(int x=0; x<m; x++) {
            for(int y=0; y<n; y++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }
}

 

  方法重載:
    在同一個類中,方法名相同,參數列表不一樣。與返回值類型無關。

    需求:我要求數的和

      咱們的需求不斷的發生改變,咱們就對應的提供了多個求和的方法。
      可是呢,他們的名字是不同的。
      而咱們又要求方法命名作到:見名知意。
      可是,很明顯,如今沒有作到。
      那麼,怎麼辦呢?
  針對這種狀況:(方法的功能相同,參數列表不一樣的狀況)

    爲了見名知意,Java容許它們起同樣的名字。  

    參數列表不一樣:
      A:參數個數不一樣
      B:參數類型不一樣

class FunctionDemo8 {
    public static void main(String[] args) {
        //jvm會根據不一樣的參數去調用不一樣的功能
        System.out.println(sum(10,20));
        System.out.println(sum(10,20,30));
        System.out.println(sum(10,20,30,40));
        
        System.out.println(sum(10.5f,20f));
    }
    
    //需求1:求兩個數的和
    public static int sum(int a,int b) {
        System.out.println("int");
        return a + b;
    }
    
    //需求2:求三數的和
    /*
    public static int sum1(int a,int b,int c) {
        return a + b + c;
    }
    */
    
    public static int sum(int a,int b,int c) {
        return a + b + c;
    }
    
    //需求3:求四個數的和
    /*
    public static int sum2(int a,int b,int c,int d) {
        return a + b + c + d;
    }
    */
    public static int sum(int a,int b,int c,int d) {
        return a + b + c + d;
    }
    
    public static float sum(float a,float b) {
        System.out.println("float");
        return a + b;
    }
}

 

  測試:

    比較兩個數據是否相等。參數類型分別爲
    兩個byte類型,兩個short類型,兩個int類型,兩個long類型,
    並在main方法中進行測試

class FunctionDemo9 {
    public static void main(String[] args) {
        //測試
        byte b1 = 3;
        byte b2 = 4;
        System.out.println("byte:"+compare(b1,b2));
        
        //測試
        short s1 = 5;
        short s2 = 5;
        System.out.println("short:"+compare(s1,s2));
                
    }
    
    //byte類型
    public static boolean compare(byte a,byte b) {
        System.out.println("byte");
        return a == b;
    }
    
    //short類型
    public static boolean compare(short a,short b) {
        System.out.println("short");
        return a == b;
    }
    
    //int類型
    public static boolean compare(int a,int b) {
        System.out.println("int");
        return a == b;
    }
    
    //long類型
    public static boolean compare(long a,long b) {
        System.out.println("long");
        return a == b;
    }
}
相關文章
相關標籤/搜索