2013-04-07 44 views
1

請考慮下面的代碼:變量不會被繼承嗎?

abstract class ClassAbstract { 
    static int _someValue = 10; 

    ClassAbstract() {} 
    } 

    class NormalClass extends ClassAbstract { 
    NormalClass(): super() {} 

    void RunMe() { 
     print("Value from abstract super: $_someValue"); // Error here 
    } 
    } 


    void main() { 
    NormalClass normalClass = new NormalClass(); 
    normalClass.RunMe(); 
    print("Application close"); 
    } 

當我運行此我收到一個錯誤:

Class 'NormalClass' has no instance getter '[email protected]'. 

NoSuchMethodError : method not found: '[email protected]' 
Receiver: Instance of 'NormalClass' 
Arguments: [] 

也許我錯了這裏,但全球性的,靜態變量必須繼承但是從這個例子可以看到它不...

回答

1

靜態成員確實是不是在Dart中繼承。如果你想在他們的聲明類之外訪問它們,你總是必須使用類名來限制訪問。

你的情況:

class NormalClass extends ClassAbstract { 
    void runMe() { 
    print("Value from abstract super: ${ClassAbstract._someValue}"); 
    } 
} 

的規範有幾句話在第7.7(非規範的一部分)說這個:

Inheritance of static methods has little utility in Dart. Static methods cannot be overridden. Any required static function can be obtained from its declaring library, and there is no need to bring it into scope via inheritance. Experience shows that developers are confused by the idea of inherited methods that are not instance methods.

Of course, the entire notion of static methods is debatable, but it is retained here because so many programmers are familiar with it. Dart static methods may be seen as functions of the enclosing library.