如何在Matlab中访问超类的常量属性?

Jam*_*rtz 3 oop matlab inheritance subclass superclass

我有一个简单的类结构,如下所示:

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
Run Code Online (Sandbox Code Playgroud)

然后由子类继承,如此.

classdef sub < super
    properties
        PROP3 = 7;
    end

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

        end
    end
end
Run Code Online (Sandbox Code Playgroud)

我的问题是当我尝试访问超级的属性PROP1或者PROP2我似乎无法访问时:

类sub没有合适的方法,属性或字段PROP1.

有没有办法在Matlab中访问super的属性?

Ale*_*xey 7

在超类中super设置属性属性

properties(Constant = true, Access = protected)
Run Code Online (Sandbox Code Playgroud)

文档中,access属性确定哪些代码可以访问这些属性:

  • public - 不受限制的访问
  • protected - 从类或子类中的方法访问
  • private - 仅按类方法访问(不是从子类访问)