看板java
标 题Re: [问题] static method
发信站中大资工二进位的世界 (Mon Jun 19 19:45:38 2006)
转信站ptt!ctu-reader!ctu-gate!news.nctu!news.ncu!news.csie.ncu!BinaryBBS
※ 引述《[email protected] (无)》之铭言:
: ※ 引述《qrtt1 (隐者)》之铭言:
: : 因为你没有正确产生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;
: : }
: : }
: 还有问题
: 虽然没产生物件
: 但是在同class 底下 使用function还要加个static
: 一直想不出为什麽一样都是class下的member function还要这样做呢?
: 谢谢~
public class
test1 {
public static void main(String args[]) {
System.out.print(
test1.more(2, 4, 4, 4));
}
public static int more(int... num) {
return ...;
}
}
public class test2 {
public void begin() {
System.out.print(
this.more(2, 4, 4, 4));
}
public int more(int... num) {
return ...;
}
}
同一个类别底下
使用static函式呼叫其他static函式时
会以自己的类别(class)作为呼叫的对象
幻想它会自动冠上
<<类别名称>>.
在这个例子中会对应到
test1.more(2, 4, 4, 4)
之所以一样都是类别下的成员函式还得加上static
是因为在非static的成员函式中呼叫其他成员函式时
会以自己的实体(instance)作为呼叫的对象
幻想它会自动冠上
this.
在这个例子中会对应到
this.more(2, 4, 4, 4)
--
珍惜现在拥有的一切
身外之物不一定能拥有一辈子...
--
〒作者:H45 来自:15-207.dorm.ncu.edu.tw
◎二进位的世界【140.115.50.50‧binary.csie.ncu.edu.tw】