2021年6月23日 星期三

JAVA 方法的使用03 方法間的呼叫

 非教學,不會詳細解說。

結論,方法可以相互呼叫,也可以呼叫自己。

 /* 方法呼叫方法測式 */

// 測試結果,方法可以呼叫方法,相互呼叫,呼叫自己,記得要設條件,不然程式跑不完。
public class ClassMethods3 {

static void funA(int i) {
System.out.println("現在在方法 A 中,呼叫方法 B 。");
i++;
funB(i);
}

static void funB(int i) {
System.out.println("現在在方法 B 中,判斷是否呼叫方法 A 。");
if ( i<3 ) {
System.out.println("呼叫方法 A 。");
funA(i);
}
else {
System.out.println("不呼叫,方法呼叫結束。");
}
}

static void funC(int i) {
System.out.println("現在在方法 C 中,判斷是否呼叫自己 。");
i++;
if ( i<3 ) {
System.out.println("呼叫方法自己。");
funA(i);
}
else {
System.out.println("不呼叫,方法呼叫結束。");
}
}

沒有留言:

張貼留言