Re: [問題] static runtime問題
※ 引述《kdok123 (小天)》之銘言:
: ex:
: class A{
: static void tell(){
: sout("I am A");
: }
: }
: class B extends A{
: static void tell(){
: super.tell(); //問題
: }
: }
: 問題:
: 有別於c++, Java的static是在runtime執行的
: 這樣我想static的運算應該會少了很多限制
: 所以我測試了一下讓static call super指針(兩個都是runtime才運算的)
: 結果發現不行?
: 其實static用起來還是跟C++一樣(至少我這麼覺得...)
: 請問既然static是在runtime運行的,那為什麼很多runtime的動作沒辦法一起做呢?
http://www.quora.com/Why-cant-static-method-access-this-or-super-in-Java
這個人解釋得較好。
Ranjith Kumar S, Computer Engineer
A static method which belongs to the class and not to the object(instance).
It can access only static data. It can not access non-static data (instance
variables). It can be accessed directly by the class name and doesn’t need
any object.
this keyword can be used to refer current class instance variable.
http://www.javatpoint.com/this-k...
super is used to refer immediate parent class instance variable.
http://www.javatpoint.com/super-...
Eventually both this and super keyword refers to instance variables. So a
static method cannot refer to “this” or “super” keywords.
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 220.135.203.156
※ 文章網址: http://www.ptt.cc/bbs/java/M.1419061919.A.576.html
推
12/20 17:02, , 1F
12/20 17:02, 1F
→
12/20 17:02, , 2F
12/20 17:02, 2F
→
12/20 17:02, , 3F
12/20 17:02, 3F
→
12/20 18:37, , 4F
12/20 18:37, 4F
→
12/20 21:48, , 5F
12/20 21:48, 5F
→
12/20 21:48, , 6F
12/20 21:48, 6F
推
12/21 12:34, , 7F
12/21 12:34, 7F
→
12/21 12:36, , 8F
12/21 12:36, 8F
→
12/21 12:37, , 9F
12/21 12:37, 9F
→
12/21 13:53, , 10F
12/21 13:53, 10F
→
12/21 13:55, , 11F
12/21 13:55, 11F
推
12/22 14:06, , 12F
12/22 14:06, 12F
→
12/22 14:07, , 13F
12/22 14:07, 13F
→
12/22 14:09, , 14F
12/22 14:09, 14F
→
12/22 14:10, , 15F
12/22 14:10, 15F
討論串 (同標題文章)
java 近期熱門文章
PTT數位生活區 即時熱門文章