Re: [問題] static method
※ 引述《hpeter (hpeter)》之銘言:
: ※ 引述《justinC (無)》之銘言:
: : public class test {
: : public static void main(String args[]) {
: : System.out.print(more(2,4,4,4));
: : }
: : public static int more(int... num)
: : {
: : int total=0;
: : for(int a:num)
: : total+=a;
: : return total;
: : }
: : }
: : 為什麼在class test底下
: : more method一定要加static 才能給過?
: : 而其他class則不需用呢?
: 小弟覺得 因為被 static function 用到的東西
: 一定要有static 之修飾
因為你沒有正確產生test物件, 所以不能呼叫test的方法
public class test {
public static void main(String args[]) {
System.out.print(new test().more(2, 4, 4, 4));
}
public int more(int... num) {
int total = 0;
for (int a : num)
total += a;
return total;
}
}
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 163.26.34.105
討論串 (同標題文章)