【正文】
? } ? 例 ? // Print the month title, . May, 1999 ? static void printMonthTitle(int year, int month) ? { ? ( +getMonthName(month)+, +year)。 ? case 7: monthName = July。 (Factorial of 5 is + factorial(n))。 等到開(kāi)始返回時(shí)再由堆棧中取出未完成的部分繼續(xù)執(zhí)行,此時(shí)被占用的堆棧才會(huì)一一被釋放。 ? } ? 49 遞歸方法 方法也可以用在遞歸 (recursive),所謂遞歸就是方法本身自己調(diào)用自己。 ? case 5: monthName = May。 i++) ? { ? if (i 10) ? ( + i)。 ? ? return total。 ? ? // Print calendar for the month of the year ? printMonth(year, month)。 // Store the sum of the numbers ? double squareSum = 0。 ? ? ( After swapping n1 is + n1 ? + n2 is + n2)。調(diào)用方法時(shí),需要提供實(shí)參,他們必須與方法中所對(duì)應(yīng)的形參次序相同,這叫參數(shù)順序匹配。 05 int height。 ( 3) 大小適中 。 換言之:方法是類的主要組成部分。 對(duì)于方法定義中的每一個(gè)參數(shù) , 方法調(diào)用時(shí)必須有一個(gè)參量與之對(duì)應(yīng) , 而且該參量的類型必須與對(duì)應(yīng)參數(shù)類型相一致 。 29 } 30 } /* app73 OUTPUT area=50 perimeter=30 */ 方法的運(yùn)行過(guò)程 例 max方法 ? public class TestMax ? { ? // Main method ? public static void main(String[] args) ? { ? int i = 5。 ? // Invoke the swap method to attempt to swap two variables ? swap(num1, num2)。 ? } ? ? // Find the max between two double values ? static double max(double num1, double num2) ? { ? if (num1 num2) ? return num1。 ? // Find standard deviation ? double deviation = ((squareSum mean)/(10 1))。 ? ? // Get the total days from 1800 to year 1 ? for (int i = 1800。 ? ? return false。 break。 break。 例 Fibonacci數(shù)列 ? public class ComputeFibonacci ? { ? // Main method ? public static void main(String args[]) ? { ? // Read the index ? (Enter an index for the Fibonacci number)。 ? // Find and display the Fibonacci number ? (Fibonacci number at index + n + is +fib(n))。 break。 break。 ? for (i = 0。 i++) ? if (isLeapYear(i)) ? total = total + 366。 ? (The standard deviation is + deviation)。 ? } ? ? ?// Find the max among three double values ? static double max(double num1, double num2, double num3) ? { ? return max(max(num1, num2), num3)。 ? } ? 例 // The method for swapping two variables ? static void swap(int n1, int n2) ? { ? ( Inside the swap method)。 ? int k = max(i, j)。 方法體通常起到兩種作用 , 一是圍繞類的屬性進(jìn)行各種操作;二是與其他的類與對(duì)象進(jìn)行數(shù)據(jù)交流 、 消息傳遞等操作 。 方法即是 JAVA創(chuàng)建一個(gè)有名字的子程序。 引言 Java程序的基本結(jié)構(gòu)如下: 引入 Java類庫(kù); 定義用戶類 1 { 定義類 1的若干變量或?qū)ο螅?