Java自定義方法、返回值、及方法重載


1 自定義方法
1.1 方法的概念

問題:為什么要有方法?

原因:當某個功能被多次(重復)使用的時候,為了簡化代碼,方便操作,通常會將該功能封裝起來,直接使用,用戶可以不用考慮功能的實現細節,只需要知道該功能如何使用即可

問題:方法定義好之后一定要使用嘛(如果不用,為啥要寫)?

原因:功能的定義並非是一定需要去使用的,定義方法的目的是在后續的使用過程中有可能會用到該功能

問題:定義好的功能,如果要使用一定要通過main函數嘛?

原因:如果是需要直接運行的功能那么必須通過主函數,如果不是直接運行的而是通過其他方法間接運行的,那么考慮的是其所在的方法是否需要直接在主函數中運行

方法與方法之間的關系:並列的、平級的關系,所有的方法都在類中,方法中不能包含另一個方法,但是可以調用另一個方法

1.2 方法的語法

修飾符 返回值類型 方法名(參數類型 參數名,...){執行代碼;}

例如:public static void main(String[] args){}

需求:定義功能實現比較兩個數的大小(求較大值)

public static void main(String[] args){
//調用方法
max();
a();
reaper();
}

public static void max(){
int x = 3;
int y = 4;
if(x>y){
    System.out.println(x);
}else{
    System.out.println(y);
}
}

public static void reaper(){
System.out.println("加滿油,點火,開始收割");
System.out.println("10分鍾之后....");
System.out.println("收麥子了,一斤1.12");
}

//做聯誼 需要有班主任的協助
public static void a(){
b();
System.out.println("聯誼成功");
}

//代表班主任的協助功能
public static void b(){
System.out.println("協助聯誼");
}
1.3 方法的參數

方法中的參數可以是無限個,根據情況來定,注意點同一個類中方法名不允許一樣,如果想要一樣就需要符合方法重載的規則,在調用方法傳值時,參數的類型和位置要和方法中定義的參數類型和位置一致

public static void main(String[] args){
   max();
   boom("banana");
}
//求兩個數的較大值
public static void max(int x,int y){
if(x>y){
       System.out.println(x);
  }else{
       System.out.println(y);
  }
}

//榨汁機
public static void boom(String fruit){
   switch(fruit){
       case "apple":
           System.out.println("先生您的"+fruit+"好了");
           break;
       case "banana":
           System.out.println("先生您的"+fruit+"好了");
           break;
       case "orange":
           System.out.println("先生您的"+fruit+"好了");
           break;
       case "pear":
           System.out.println("先生您的"+fruit+"好了");
           break;

  }
}

//求三個數的較大值
public static void max2(int x,int y,int z){
   if(x>y){
  if(x>z){
           System.out.println(x);
      }else{
           System.out.println(z);
      }
  }else{
       if(y>z){
           System.out.println(y);
      }else{
           System.out.println(z);
      }
  }
}

形參和實參

形參:形式參數,指的就是方法中定義的參數,是沒有具體值的,存在的意義是告訴調用者想要使用該功能就必須給到相應類型的值

實參:實際參數,指的是調用者在調用某個方法時,傳遞的實際的具體的值

public static void main(String[] args){
   int x = 5;
   int y = 6;
   max(x,y);//實參
}

public static void max(int x,int y){ //形參
   if(x>y){
       System.out.println(x);
  }else{
       System.out.println(y);
  }
}

參數類型轉換 :小類型的轉大類型的可以,大類型轉小類型需要強制類型轉換,否則編譯失敗

public static void main(String[] args){
byte a = 7;
int b = 6;
//max(a,b);
max3(a,b);
}

public static void max(int x,int y){
if(x>y){
System.out.println(x);
}else{
System.out.println(y);
}
}

//byte類型為參數,傳遞int類型會報錯(從int轉換到byte可能會有損失)
int a = 7;
int b = 6;
public static void max2(byte x,int y){
if(x>y){
System.out.println(x);
}else{
System.out.println(y);
}
}

//小類型傳遞給大類型是正常的
public static void max3(int x,int y){
if(x>y){
System.out.println(x);
}else{
System.out.println(y);
}
}
1.4 方法的返回值

問題:為什么方法需要返回值?

原因:當需要對方法中返回的結果進行二次處理的時候,就需要返回值

問題:什么時候需要給具體的返回值?

原因:看需求(需要對結果再次進行處理的時候,那就給返回值,相反則不給[無返回類型--void])

關鍵詞:return(返回) void(無返回值類型)

return作用:1、返回結果 2、結束方法(函數)

需求:求某學生的語數英三門成績的最大值,然后給出該成績對應的科目評級

//所有的數據都需要有特定的數據類型,因此要返回的數據也需要指定數據類型
public static int maxScore(int a,int b,int c){
   if(a>b){
       if(a>c){
           return a;
      }else{
           return c;
      }
  }else{
       if(b>c){
           return b;
      }else{
           return c;
      }
  }
}
//方案一:得到返回值在主函數中處理業務 --不推薦,不建議在主函數中寫業務邏輯
public static void main(String[] args){
int x = 8;
int y = 65;
int z = 55;
int score = maxScore(x,y,z);
if(score>90){
System.out.println("優秀");
}else if(score>70){
System.out.println("良好");
}else if(score>59){
System.out.println("及格");
}else{
System.out.println("不及格");
}

}

//所有的數據都需要有特定的數據類型,因此要返回的數據也需要指定數據類型
public static int maxScore(int a,int b,int c){
   if(a>b){
       if(a>c){
           return a;
      }else{
           return c;
      }
  }else{
       if(b>c){
           return b;
      }else{
           return c;
      }
  }
}
//方案二:不需要返回值,直接在方法中處理業務邏輯
public static void main(String[] args){
   maxScore1(55,78,66);
}

public static void maxScore1(int a,int b,int c){
   if(a>b){
       if(a>c){
           print(a);
      }else{
           print(c);
      }
  }else{
       if(b>c){
           print(b);
      }else{
           print(c);
      }
  }
}

//根據給成績判斷出該成績的等級
public static void print(int score){
   if(score>90){
       System.out.println("優秀");
  }else if(score>70){
       System.out.println("良好");
  }else if(score>59){
       System.out.println("及格");
  }else{
       System.out.println("不及格");
  }
}
//方案三:對方案二進行優化 減少方法的調用次數
public static void main(String[] args){
   print(maxScore(55,78,66));
}

//所有的數據都需要有特定的數據類型,因此要返回的數據也需要指定數據類型
public static int maxScore(int a,int b,int c){
   if(a>b){
       if(a>c){
           return a;
      }else{
           return c;
      }
  }else{
       if(b>c){
           return b;
      }else{
           return c;
      }
  }
}

public static void maxScore1(int a,int b,int c){

   int max = 0;
   if(a>b){
       if(a>c){
           max = a;
      }else{
           max = c;
      }
  }else{
       if(b>c){
           max = b;
      }else{
           max = c;
      }
  }
   print(max);
}

//根據給成績判斷出該成績的等級
public static void print(int score){
   if(score>90){
       System.out.println("優秀");
  }else if(score>70){
       System.out.println("良好");
  }else if(score>59){
       System.out.println("及格");
  }else{
       System.out.println("不及格");
  }
}
2 方法的重載

概念:在同一個類中,方法名相同,方法的參數列表不同(參數個數、參數位置、參數的類型),與返回值類型無關

問題:為什么需要所謂的方法的重載?

原因:多個方法具有相同的功能特性,但是呢,每個功能的實現方式可能不同

//需求:求較大值(兩個數)
public static int max(int x,int y){
   if(x>y){
       System.out.println(x);
  }else{
      System.out.println(y);
  }
}
//求較大值(三個數)
public static int max(int x,int y,int z){
   if(x>y){
  if(x>z){
           System.out.println(x);
      }else{
           System.out.println(z);
      }
  }else{
       if(y>z){
           System.out.println(y);
      }else{
           System.out.println(z);
      }
  }
}
//案例
public static void main(String[] args){
   int x = 5;
   int y = 6;
   int z = 7;
   max(x,y,z);
}

//需求:求較大值(兩個數)
public static void max(int x,int y){
   System.out.println("-----------");
   if(x>y){
       System.out.println(x);
  }else{
       System.out.println(y);
  }
}
//類型不同
public static void max(byte a,int b){
   if(a>b){
       System.out.println(a);
  }else{
       System.out.println(b);
  }
}

/*public static int max(int a,byte b){
if(a>b){
System.out.println(a);
}else{
System.out.println(b);
}
return 0;
}*/
//位置不同
public static void max(int a,byte b){
   if(a>b){
       System.out.println(a);
  }else{
       System.out.println(b);
  }
}
//個數不同
//求較大值(三個數)
public static void max(int x,int y,int z){
   if(x>y){
       if(x>z){
           System.out.println(x);
      }else{
           System.out.println(z);
      }
  }else{
       if(y>z){
           System.out.println(y);
      }else{
           System.out.println(z);
      }
  }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM