1、方法概述
假設有一個遊戲程序,程序在運行過程當中,要不斷地發射炮彈(植物大戰殭屍)。發射炮彈的動做須要編寫100行的代碼,在每次實現發射炮彈的地方都須要重複地編寫這100行代碼,這樣程序會變得很臃腫,可讀性也很是差。爲了解決代碼重複編寫的問題,能夠將發射炮彈的代碼提取出來放在一個{}中,併爲這段代碼起個名字,這樣在每次發射炮彈的地方經過這個名字來調用發射炮彈的代碼就能夠了。上述過程當中,所提取出來的代碼能夠被看做是程序中定義的一個方法,程序在須要發射炮彈時調用該方法便可。jvm
簡單的說:方法就是完成特定功能的代碼塊。在不少語言裏面都有函數的定義,函數在Java中被稱爲方法。函數
2、方法的格式
修飾符 返回值類型 方法名(參數類型 參數名1,參數類型 參數名2...) { 方法體語句; return 返回值; }
詳細解釋:測試
修飾符:目前就用 public static。後面咱們再詳細的講解其餘的修飾符。spa
返回值類型:就是功能結果的數據類型。code
方法名:符合命名規則便可。方便咱們的調用。對象
參數:blog
實際參數:就是實際參與運算的。
形式參數;就是方法定義上的,用於接收實際參數的。遊戲
參數類型:就是參數的數據類型get
參數名:就是變量名虛擬機
方法體語句:就是完成功能的代碼。
return:結束方法的。
返回值:就是功能的結果,由return帶給調用者。
【如何寫一個方法?】——兩個明確
- 返回值類型:結果的數據類型
- 參數列表:你要傳遞幾個參數,以及每一個參數的數據類型
/* 需求:求兩個數據之和的案例 兩個明確: 返回值類型: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; }
3、方法的調用
3.1 有明確返回值的方法調用
A:單獨調用,通常來講沒有意義,因此不推薦。
B:輸出調用,可是不夠好。由於咱們可能須要針對結果進行進一步的操做。
C:賦值調用,推薦方案。
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); }
【方法的注意事項】
A:方法不調用不執行
B:方法與方法是平級關係,不能嵌套定義
C:方法定義的時候參數之間用逗號隔開
D:方法調用的時候不用在傳遞數據類型
E:若是方法有明確的返回值,必定要有return帶回一個值
class FunctionDemo2 { 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; } }
【練習】
鍵盤錄入兩個數據,返回兩個數中的較大值
public class FunctionTest { 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) { return a; } else { return b; }*/ //用三元改進 /*int c = a > b ? a : b; return c;*/ //因爲c就是後面的式子 return ((a > b) ? a : b); } }
鍵盤錄入兩個數據,比較兩個數是否相等
public class FunctionTest2 { 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 * 參數列表: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; } }
鍵盤錄入三個數據,返回三個數中的最大值
public 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; } }
3.2 沒有明確返回值的方法調用
void類型返回值的方法調用:
單獨調用
輸出調用(錯誤)
賦值調用(錯誤)
/* 需求:在控制檯輸出以下的形狀 ***** ***** ***** ***** */ public class FunctionDemo3 { 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(); } } }
【練習】
鍵盤錄入行數和列數,輸出對應的星形
public class FunctionTest4 { 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 i = 0; i < m; i++) { for (int j = 0; j < n; j++) { System.out.print("* "); } System.out.println(); } } }
鍵盤錄入一個數據n(1<=n<=9),輸出對應的nn乘法表
public class FunctionTest5 { public static void main(String[] args) { //建立對象 Scanner sc = new Scanner(System.in); System.out.println("請輸入n的值:(1~9)"); int n = sc.nextInt(); //調用 printNN(n); } /** * 需求:輸出對應的nn乘法表 * 兩個明確: * 返回值類型:void * 參數列表:int n */ public static void printNN(int n) { for (int x = 1; x <= n; x++) { for (int y = 1; y <= x; y++) { System.out.print(y + "*" + x + "=" + y * x + "\t"); } System.out.println(); } } }
4、方法重載
需求:我要求數的和
public class FunctionDemo4 { //需求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; } //需求3:求四個數的和 public static int sum2(int a,int b,int c,int d) { return a + b + c + d; } }
咱們的需求不斷的發生改變,咱們就對應的提供了多個求和的方法。可是呢,他們的名字是不同的。而咱們又要求方法命名作到:見名知意。
可是,很明顯,如今沒有作到。那麼,腫麼辦呢?
針對這種狀況:方法的功能相同,參數列表不一樣的狀況,爲了見名知意,Java容許它們起同樣的名字。
其實,這種狀況有一個專業名詞:方法重載。
【方法重載概述】
在同一個類中,容許存在一個以上的同名方法,只要它們的參數個數或者參數類型不一樣便可。
【方法重載特色】
- 與返回值類型無關,只看方法名和參數列表
- 在調用時,虛擬機經過參數列表的不一樣來區分同名方法
public class FunctionDemo4 { 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 sum(int a,int b,int c) { return a + b + c; } //需求3:求四個數的和 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類型
public class FunctionTest6 { 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; } }