訪問修飾符 返回類型 方法名(參數列表){ 方法體; }
方法分類:java
/** * 使用方法進行星號打印 */ public class MethodDemo { //打印輸出星號 public void printStar() { System.out.println("**********************"); } public static void main(String[] args) { //建立MethodDemo類的對象methodDemo MethodDemo methodDemo = new MethodDemo(); //使用對象名.方法名()調用方法 methodDemo.printStar(); System.out.println("歡迎來到Java的世界"); methodDemo.printStar(); } }
/** * 使用方法計算矩形面積 */ public class RectangleArea { //求矩形面積的方法 public int area() { int length = 10; int width = 5; int calArea = length * width; return calArea;//返回語句返回面積 } public static void main(String[] args) { RectangleArea rectangleArea = new RectangleArea(); System.out.println("矩形的面積爲:" + rectangleArea.area()); } }
/** * 使用方法求兩個float類型數據的最大值,並在方法中將最大值輸出 */ public class MaxDemo { //求最大值的方法 public void max(float a, float b) { float max; if (a > b) { max = a; } else { max = b; } System.out.println("最大值是:" + max); } public static void main(String[] args) { MaxDemo maxDemo = new MaxDemo(); float m = 2.1f, n = 3.7f; maxDemo.max(m, n); } }
/** * 定義一個求n!的方法 * 而後計算1!+2!+3!+4!+5! */ public class FacDemo { //求n!的階乘 public int fac(int n){ int s = 1; for(int i = 1; i <= n; i++){ s *= i; } return s; } public static void main(String[] args) { FacDemo facDemo = new FacDemo(); //求1!+2!+3!+4!+5! int sum = 0; for(int i = 1; i <= 5; i++){ sum += facDemo.fac(i); } System.out.println("階乘結果爲:"+sum); } }
/** * 使用方法打印輸出數組的元素 */ public class ArrayMethod { //輸出數組元素的方法 public void printArray(int[] arr) { for (int n : arr) { System.out.print(n + " "); } System.out.println();//格式調整 } public static void main(String[] args) { ArrayMethod arrayMethod = new ArrayMethod(); int[] array = {10, 20, 30, 40, 50}; arrayMethod.printArray(array); } }
import java.util.Scanner; /** * 使用方法查找數組元素的值 */ public class ArraySearch { //查找數組元素值的方法 public boolean search(int n, int[] arr) { boolean flag = false;//默認沒找到 for (int m : arr) { if (m == n) { flag = true; break;//使用break語句提升程序的效率 } } return flag; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); ArraySearch arraySearch = new ArraySearch(); int[] array = {1, 2, 3, 4, 5}; System.out.println("請輸入要查詢的值:"); int n = sc.nextInt(); boolean flag = arraySearch.search(n, array); if (flag) { System.out.println("找到了"); } else { System.out.println("沒找到"); } } }
/** * 定義三個方法,實現int、double和數組類型和的問題 */ public class MathDemo { //求兩個int類型數的和 public int plus(int a, int b) { return a + b; } //求兩個double類型的數的和 public double plus(double a, double b) { return a + b; } //求數組元素的累加和 public int plus(int[] arr) { int sum = 0; for (int n : arr) { sum += n; } return sum; } public static void main(String[] args) { int m = 5, n = 10; int[] array = {1, 2, 3, 4, 5, 6}; MathDemo mathDemo = new MathDemo(); System.out.println("int類型的和:" + mathDemo.plus(m, n)); System.out.println("double類型的和:" + mathDemo.plus(m, n)); System.out.println("數組元素的累加和:" + mathDemo.plus(array)); } }
案例一:數組
public class ExchangeDemo1 { //定義交換方法 public void swap(int a, int b) { int temp; System.out.println("交換前: a = " + a + ", b = " + b); temp = a; a = b; b = temp; System.out.println("交換後: a = " + a + ", b = " + b); } public static void main(String[] args) { int m = 4, n = 5; ExchangeDemo1 swapDemo = new ExchangeDemo1(); System.out.println("交換前: m = " + m + ", n = " + n); swapDemo.swap(m, n); System.out.println("交換後: m = " + m + ", n = " + n); }
案例二:code
public class ExchangeDemo2 { public void add(int n) { n++; System.out.println("方法中n的值:" + n); } public static void main(String[] args) { int n = 10; System.out.println("方法調用前n的值:" + n); ExchangeDemo2 exchangeDemo2 = new ExchangeDemo2(); exchangeDemo2.add(n); System.out.println("方法調用後n的值:" + n); } }
public class ArrayDemo { //定義一個用於修改數組某個元素值的方法 public void update(int[] arr) { arr[3] = 15; System.out.println("修改後數組arr的元素爲:"); for (int n : arr) { System.out.print(n + " "); } System.out.println(); } public static void main(String[] args) { ArrayDemo arrayDemo = new ArrayDemo(); int[] array = {1, 2, 3, 4, 5}; System.out.println("方法調用前數組array的元素爲;"); for (int n : array) { System.out.print(n + " "); } System.out.println(); arrayDemo.update(array); System.out.println("方法調用後數組array的元素爲;"); for (int n : array) { System.out.print(n + " "); } System.out.println(); } }
案例一:對象
public class ArgsDemo1 { //求和 public void sum(int... n) { int sum = 0; for (int i : n) { sum += i; } System.out.println("sum = " + sum); } public static void main(String[] args) { ArgsDemo1 argsDemo = new ArgsDemo1(); argsDemo.sum(1); argsDemo.sum(1, 2); argsDemo.sum(1, 2, 3); } }
案例二:參數列表中若是有兩個以上的參數,可變參數必定是在最後;能夠將數組傳遞給可變參數列表,可是數組做爲參數時,是不能將多個值傳遞給數組的ip
public class ArgsDemo2 { //查找 public void search(int n, int... a) { boolean flag = false; for (int i : a) { if (i == n) { flag = true; break; } } if (flag) { System.out.println("找到了"); } else { System.out.println("沒找到"); } } public static void main(String[] args) { ArgsDemo2 argsDemo2 = new ArgsDemo2(); argsDemo2.search(3, 1, 2, 3, 4, 5); int[] a = {4, 5, 6, 7, 8}; argsDemo2.search(3, a); } }
Tips:若此時定義另外一個search方法 -- public void search(int n, int[] array)
,並非方法重載而是重複定義class
案例:效率
public class ArgsDemo3 { public int plus(int a, int b) { System.out.println("不帶可變參數列表的方法被調用"); return a + b; } public int plus(int... a) { int sum = 0; for (int i : a) { sum += i; } System.out.println("帶可變參數列表的方法被調用"); return sum; } public int plus(int[] arr, int... a) { int sum1 = 0, sum2 = 0; for (int i : arr) { sum1 += i; } for (int i : a) { sum2 += i; } return sum1 + sum2; } public static void main(String[] args) { ArgsDemo3 argsDemo3 = new ArgsDemo3(); System.out.println("和爲:" + argsDemo3.plus(1, 2)); int[] arr = {1, 2, 3}; System.out.println("可變參數列表和數組同時做爲方法參數" + argsDemo3.plus(arr, 1, 2, 3, 4)); } }