2013-03-20 43 views
3

我有一個簡單的類結構,如下所示:如何在Matlab中訪問超類的常量屬性?

classdef super < hgsetget 
    properties(Constant = true, Access = private) 
     PROP1 = 1; 
     PROP2 = {2 [3 4] [5 6]}; 
    end 

    methods 
     function self = super() 
      // Constructor code here 
      // ... 
     end 
    end 
end 

,然後由子類繼承了像這樣。

classdef sub < super 
    properties 
     PROP3 = 7; 
    end 

    methods 
     function self = sub() 
      // Subclass constructor here 
      // ... 
      self = [email protected](); 
      test = self.PROP1; // I don't appear to have access to PROP1 from Super 

     end 
    end 
end 

我的問題是,當我嘗試進入超級財產PROP1PROP2我似乎並沒有得到訪問:

沒有適當的方法,屬性或字段PROP1類子。

有沒有辦法在Matlab中訪問超級屬性?

回答

7

在超super組屬性屬性

properties(Constant = true, Access = protected) 

the documentation訪問屬性決定了哪些代碼可以訪問這些屬性:

  • 公共 - 無限制訪問
  • 保護 - 從方法訪問類或子類
  • private - 通過類方法訪問onl y(不是來自亞類)
2

您將屬性定義爲private,這些屬性不是繼承的。

改爲使用Access = protected