Re: [問題] 建構子問題

看板java作者 (XDDD)時間13年前 (2012/03/15 16:33), 編輯推噓2(200)
留言2則, 1人參與, 最新討論串6/7 (看更多)
: 我想還是看用途吧,假設今天是: : class something { : private static int instance_count = 0; : pubilc something() { : instance_count++; : // TODO more thing : } : } : 就不奇怪了吧 : 在強調一次,程式寫法各有不同,重點是要清楚自己在做啥 這樣子的話是表示如果要使用到 instance_count 一定要透過 something() 如果宣告 static int instance_count 是代表 class something { static int instance_count = 0; public a() {} public b() {} } a() 和 b() 都可以使用到instance_count, 而且運算過的值不會是正確的 因為b()使用到個instance_count有可能是a()運算過後的結果.. 如果宣告int instance_count, a() b()讀到的值都是初始化過的 是這樣嗎? 另外還有另個問題 內部是否有隱藏一個 this.instance_count = instance_count; class something { private static int instance_count = 0; pubilc something() { instance_count++; // TODO more thing } } 謝謝 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 114.33.148.237

03/15 16:43, , 1F
這樣子的話是表...something() <= 這是因為private的關係
03/15 16:43, 1F

03/15 16:47, , 2F
內部是否...count; <= 要這樣想也OK,但不建議那樣用static
03/15 16:47, 2F
文章代碼(AID): #1FOQbarW (java)
討論串 (同標題文章)
文章代碼(AID): #1FOQbarW (java)