延續前一天函數的內容,我們今天繼續介紹「引數與參數」。
這邊先簡單複習一下兩者最大的差異 :
以下利用附上的程式碼繼續介紹引數參數和使用方法 :
public class First{ /*first class*/
static void statment(){
System.out.println("Hello, Classes !!!");
}
static void average(int gradeA, int gradeB, int gradeC){
float num;
num = ((float)x + (float)y + (float)z)/3; //強制轉型
return num;
}
static float calculate(int x, int y, int z){ // x & y are Parameters
float ave;
System.out.println("The lowest grade is : " + Math.min(x,y,z));
ave = average(x,y,z);
return ave;
}
/*public int sum(int a, int b,int c){
int summary = a + b + c;
return summary;
}*/
}
public class Second{ //second class
public static void main(String[] args){
int aStudent = 92;
int bStudent = 75;
int cStudent = 68;
First.statment(); // Using method in the other "class"
float average = calculate(aStudent, bStudent, cStudent);
System.out.println("The average is " + average);
/*System.out.println("The total is " + sum(aStudent, bStudent, cStudent));*/
}
}
First total = new First(); //Class_Name Object_name = new Class_name() ;
System.out.println("The total is " + total.sum(aStudent, bStudent, cStudent));
後續會有更詳細關於物件的介紹,此處先記得創造一個新物件可能會用到keyword new
以上內容若有錯誤,煩請不吝嗇告知,謝謝您!!!