简体   繁体   中英

How do I access a superclass's constant property within Matlab?

I have a simple class structure like so:

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

which is then inherited by a subclass like so.

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

My issue is when I try to access to the super's property PROP1 or PROP2 I don't seem to get access:

No appropriate method, property, or field PROP1 for class sub.

Is there a way to access to the super's property within Matlab?

In superclass super set properties attributes to

properties(Constant = true, Access = protected)

From the documentation an access attribute determines what code can access these properties:

  • public — Unrestricted access
  • protected — Access from methods in class or subclasses
  • private — Access by class methods only (not from subclasses)

You define the properties as private , those are not inherited.

Use Access = protected instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM