作者justinC (无)
看板java
标题Re: [问题] static method
时间Mon Jun 19 22:54:25 2006
※ 引述《[email protected] (大家来创社 ^^//)》之铭言:
: ※ 引述《[email protected] (无)》之铭言:
: : 还有问题
: : 虽然没产生物件
: : 但是在同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)
这个解释就很容易懂
那static method 不能存取non-static的变数
也是这个道理~
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 61.221.120.127