○ 為一群相同名稱的方法,依照參數型別不同而去分別找出相對應的方法
=============================================================================================
public class Overloading {
static void fruit(){
System.out.println("apple");
}
static void fruit(String name){
System.out.println("I like eat "+ name+".");
}
static void fruit(int n, String name){
for (int i=0;i<n;i++) //利用for迴圈跑出n次
System.out.println("There are have"+" "+ name +" in the basket.");
}
public static void main(String [] args){
fruit();
fruit("grape");
fruit(3,"oranges"); // int n,3 為重複次數
}
}
=============================================================================================
result
apple
I like eat grape
There are have oranges in the basket.
There are have oranges in the basket.
There are have oranges in the basket.
result
apple
I like eat grape
There are have oranges in the basket.
There are have oranges in the basket.
There are have oranges in the basket.
沒有留言:
張貼留言