Re: [問題] static method
※ 引述《H45.bbs@binary.csie.ncu.edu.tw (大家來創社 ^^//)》之銘言:
: ※ 引述《justinC.bbs@ptt.cc (無)》之銘言:
: : 還有問題
: : 雖然沒產生物件
: : 但是在同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
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 8 之 8 篇):