1

コンストラクターに提供されるか、他のメソッドで生成される可能性のあるプロパティを持つクラスを実装しようとしています。データをディスクに保存したり、ロード時に生成したりしたくありません。私がこれまでに持っているものは次のとおりです。

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 の呼び出しを回避するにはどうすればよいですか?

4

2 に答える 2

1

プロパティは (つまり、コンストラクターで) 設定できる場合があるため、このプロパティは厳密に依存しているわけではありません。1 つの解決策は、コンストラクターのプライベート プロパティ (CustomProblemProperty以下の例) に設定可能な値を格納することです。のgetメソッドは、ProblemProperty空でない場合はこのプライベート プロパティの値を返し、それ以外の場合は生成された値を返します。

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
于 2011-07-28T22:15:42.427 に答える
1

あなたのソリューションは機能していますが、それは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 はどこにも保存されず、標準コードを記述できるという利点があります。

于 2012-01-04T09:59:33.170 に答える