5

としてアクセスできる読み取り専用フィールドが必要ですfv=object.fieldが、返される値はオブジェクトの他のフィールドから計算されます (つまり、戻り値は を満たしますfv==f(object.field2))。

property必要な機能は、 Python の関数/デコレータと同じです。

ブロックのパラメーターを設定することでこれが可能になるというリファレンスを見たのを思い出しますpropertiesが、Matlab OOP のドキュメントは散らばっていて、もう一度見つけることができません。

4

2 に答える 2

5

これは「依存」プロパティと呼ばれます。派生プロパティを使用するクラスの簡単な例を以下に示します。

classdef dependent_properties_example < handle       %Note: Deriving from handle is not required for this example.  It's just how I always use classes.
    properties (Dependent = true, SetAccess = private)
        derivedProp
    end
    properties (SetAccess = public, GetAccess = public)
        normalProp1 = 0;
        normalProp2 = 0;
    end
    methods
        function out = get.derivedProp(self)
            out = self.normalProp1 + self.normalProp2;
        end
    end
end

このクラスを定義すると、次を実行できるようになります。

>> x = dependent_properties_example;
>> x.normalProp1 = 3;
>> x.normalProp2 = 10;
>> x
x = 
  dependent_properties_example handle

  Properties:
    derivedProp: 13
    normalProp1: 3
    normalProp2: 10
于 2013-06-13T15:11:57.077 に答える
2

プロパティ アクセス メソッドを使用できます: http://www.mathworks.co.uk/help/matlab/matlab_oop/property-access-methods.html

get/set 関数を定義するには、get 関数を使用して、他のメンバーから計算された値を返すことができる必要があります。上記のリンクの「依存プロパティで Set メソッドを使用する場合」セクションに、この例が示されています。

于 2013-06-13T15:00:57.210 に答える