Matlab OOP 在保存瞬态属性时调用 get 方法。

发布于 2024-11-27 00:39:04 字数 1250 浏览 1 评论 0原文

我正在尝试实现一个类,该类的属性可以提供给构造函数或可能在其他方法中生成。我不希望将数据保存到磁盘或在加载时生成。到目前为止我所遇到的是:

classdef MyClass
        properties(GetAccess = public, SetAccess = private)
            Property1
            Property2
            Property3
        end
        properties(Access = private)
            Property4
        end
        properties(Transient = true)
            ProblemProperty
        end
        properties(Dependent = true, Transient = true)
            Property5
        end

        methods
            function MyClass
                % Constructor.
            end

            function val = get.Property5(B)
                val = SomeFunction(Property1);
            end

            function val = get.ProblemProperty(B)
                if isempty(B.ProblemProperty)
                    B = GenerateProblemProperty(B);
                end
                val = B.ProblemProperty;
            end

            function B = GenerateProblemProperty(B)
                B.ProblemProperty = AnotherFunction(B.Property2);
            end
        end
    end

问题是,当我尝试将对象保存到磁盘时,Matlab 调用 get.ProblemProperty 方法(通过仅在 save 语句上运行探查器来确认)。 ProblemProperty 字段为空,我希望它保持这种状态。它不调用 get.Property5 方法。

如何避免调用 get.ProblemProperty?

I'm trying to implement a class with a property that can be possibly provided to the constructor or possibly generated in some other method. I don't want the data saved to disk or generated on load. What I have so far is:

classdef MyClass
        properties(GetAccess = public, SetAccess = private)
            Property1
            Property2
            Property3
        end
        properties(Access = private)
            Property4
        end
        properties(Transient = true)
            ProblemProperty
        end
        properties(Dependent = true, Transient = true)
            Property5
        end

        methods
            function MyClass
                % Constructor.
            end

            function val = get.Property5(B)
                val = SomeFunction(Property1);
            end

            function val = get.ProblemProperty(B)
                if isempty(B.ProblemProperty)
                    B = GenerateProblemProperty(B);
                end
                val = B.ProblemProperty;
            end

            function B = GenerateProblemProperty(B)
                B.ProblemProperty = AnotherFunction(B.Property2);
            end
        end
    end

The problem is that when I try to save the object to disk, Matlab calls the get.ProblemProperty method (confirmed by running the profiler on just the save statement). The ProblemProperty field is empty and I want it to stay that way. It doesn't call the get.Property5 method.

How do I avoid the call to get.ProblemProperty?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

白芷 2024-12-04 00:39:04

由于有时可以设置该属性(即在构造函数中),因此该属性不是严格依赖的。一种解决方案是将可设置值存储在构造函数中的私有属性(下例中的 CustomProblemProperty)中。然后,ProblemPropertyget 方法将检查是否返回此私有属性值(如果不为空),否则返回生成的值。

classdef MyClass
    properties(GetAccess = public, SetAccess = private)
        Property1
        Property2
        Property3
    end
    properties(Access = private)
        Property4
        CustomProblemProperty
    end
    properties(Dependent = true, Transient = true)
        ProblemProperty
        Property5
    end

    methods
        function B = MyClass(varargin)
            if nargin == 1
               B.CustomProblemProperty = varargin{1};
            end
        end

        function val = get.Property5(B)
            val = SomeFunction(Property1);
        end

        function val = get.ProblemProperty(B)
            if isempty(B.CustomProblemProperty)
               val = AnotherFunction(B.Property2);
            else
               val = B.CustomProblemProperty;
            end
        end

    end
end

Since the property can sometimes be set (i.e. in the constructor) then this property is not strictly dependent. One solution is to store the settable value in a private property (CustomProblemProperty in the example below) in the constructor. The get method of ProblemProperty would then check return this private property value if it's not empty and otherwise return the generated value.

classdef MyClass
    properties(GetAccess = public, SetAccess = private)
        Property1
        Property2
        Property3
    end
    properties(Access = private)
        Property4
        CustomProblemProperty
    end
    properties(Dependent = true, Transient = true)
        ProblemProperty
        Property5
    end

    methods
        function B = MyClass(varargin)
            if nargin == 1
               B.CustomProblemProperty = varargin{1};
            end
        end

        function val = get.Property5(B)
            val = SomeFunction(Property1);
        end

        function val = get.ProblemProperty(B)
            if isempty(B.CustomProblemProperty)
               val = AnotherFunction(B.Property2);
            else
               val = B.CustomProblemProperty;
            end
        end

    end
end
寒江雪… 2024-12-04 00:39:04

你的解决方案是有效的,但它不是 OOP 精神,你将对象的外部形状与内部的访问器混合在一起。

我建议以下

classdef simpleClass
    properties(Access = private)
        % The concrete container for implementation
        myNiceProperty % m_NiceProperty is another standard name for this.
    end
    properties(Dependent)
        % The accessor which is part of the object "interface"
        NiceProperty
    end

    method
        % The usual accessors (you can do whatever you wish there)
        function value = get.NiceProperty(this)
            value = this.myNiceProperty;
        end
        function set.NiceProperty(this, value)
            this.myNiceProperty = value;
        end
    end
end

NiceProperty 永远不会保存在任何地方,并且您可以受益于编写标准代码。

Your solution is working, but it is not the OOP spirit, you are mixing accessors which are the outside shape of the object with what is inside.

I would advise the following

classdef simpleClass
    properties(Access = private)
        % The concrete container for implementation
        myNiceProperty % m_NiceProperty is another standard name for this.
    end
    properties(Dependent)
        % The accessor which is part of the object "interface"
        NiceProperty
    end

    method
        % The usual accessors (you can do whatever you wish there)
        function value = get.NiceProperty(this)
            value = this.myNiceProperty;
        end
        function set.NiceProperty(this, value)
            this.myNiceProperty = value;
        end
    end
end

NiceProperty is then never saved anywhere and you have the benefit of writing standard code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文