※ 引述《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.