简体   繁体   中英

How do I create a class with constant properties accessible to other classes within a package folder structure?

I have the following file structure of packages for my MatLab project:

RootFolder
    +CustomUiElements
        Styles.m
        ...
    ...
    +DetailView
        Controller.m
        ...
    MainApp.m

Within the Controller.m file I'm having issues accessing the class file Styles.m which is simply a data class with properties that are structures of properties to be used as "styles" for my ui elements:

classdef Styles
    %STYLES A collection of structures used to unify the styling of the
    %   application.  

    properties (Constant = true)
        FontName = 'Helvetica';
        FontSizes = struct('Title', 16, 'SubTitle', 14, 'BodyTitle', 12, 'Body', 10);
        TabSizes = struct('Title', 260, 'LrgSubTitle', 120, 'SmlSubTitle', 75);
        PaddingSizes = struct('None', 0, 'Small', 2, 'Med', 5, 'Large', 10);

        LargeTabStyle = struct('FontName', Styles.FontName, ...
            'FontSize', Styles.FontSizes.Title, 'FontWeight', 'demi', ...
            'TabSize', Styles.TabSizes.Title);

... % and so forth

However whenever I try to call the class property, even from the console I get the following error:

>> CustomUiElements.Styles.LargeTabStyle
Undefined variable "Styles" or class "Styles.FontName".

Since I'm not even trying to access the prop FontName this is leading me to believe that this is an issue with the construction of the obj of class Styles . I didn't have any errors, before switching to a "non-packaged" folder structure. How do I create a class that has Constant props and is accessible using a package folder structure.

Edit:

I'm able to access and use other class files within that folder (CustomUiElements). However they have custom constructors, methods, and properties, which Styles only has properties that are Constant .

Are you sure you have "RootFolder" added on the path? Here is a quick test I did, which works just fine (I'm running R2013a):

root/+pkg/MyClass.m

classdef MyClass
    properties (Constant)
        Version = '1.0';
        Info = struct('Name','MyClass', 'Version',pkg.MyClass.Version);
    end
end

Now

>> addpath('/path/to/root')
>> pkg.MyClass.Version
ans =
1.0
>> pkg.MyClass.Info.Version
ans =
1.0

So I was able to even directly index into the returned structure.

Note that if you want to reference any of the constant properties inside other properties, you have to use the fully qualified name including the package name (just like we did for the "Version" property inside the structure)

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