0

特定のプロパティが設定されているときにリスナーを使用してオブジェクトを変更するクラス定義を作成しています。そのようです:

classdef MyObject < handle
    properties (SetObservable)
        orientation = 'h'; % h = horizontal, v = vertical, o = other
        length
        width
    end
    methods
        % Constructor
        function mo = MyObject(o)
            mo.orientation = o;
            addlistener(mo, 'orientation', 'PreSet', @mo.changeOrientation);
        end

        % Change Orientation Listener
        function changeOrientation(mo, src, evnt)
            celldisp(src);
            celldisp(evnt);
            % I want a way to access newor here
            if mo.orientation == 'h' && newor == 'o'
                tempw = mo.width
                mo.width = mo.length
                mo.length = tempw;
            end
        end

        % Setter
        function set.orientation(mo, newor)
            mo.orientation = newor;
        end
    end
end

向きを設定するときに変数 newor を使用できるようにしたい。新しい方向変数を changeOrientation メソッドに渡すにはどうすればよいですか?

Matlab はプロパティ (長さと幅) が初期化されていない可能性があると文句を言うためchangeOrientation、メソッドに内容を移動することは避けたいと思います。set.orientation

編集の長さは方向に依存しません。向きが変わったら、長さと幅を交換するだけです。それ以外の場合、ユーザーは長さまたは幅を任意の正の値に設定できる必要があります。

4

2 に答える 2

1

PreSetリスナーは単なるリスナーであるため、これを行うことはできません。リスナー コールバックに渡されたデータがオブジェクトに戻ることはなく、リスナー内でその値を変更しても影響はありません。

PreSet リスナーを使用する目的は、変更される前にプロパティの値を取得することですが、実際に割り当てられる前に値を変更することではありません。

あなたが投稿したコードでset.orientationは、クラスのメソッド内で向きの検証/変更を行うだけです。

function updateLength(mo)
    % Change mo.length here and others based on mo.orientation
end

function set.orientation(mo, newor)
    % validate newor
    if dovalidation(newor)
        mo.orientation = newor;
    else
        error('invalid!');
    end

    % Now trigger a "callback"
    mo.updateDependentVariables()
end

編集

あなたのコメントに基づいて、おそらくこれを行うためのより良い方法は、ユーザーが割り当てる値を保持lengthするシャドウ プロパティを作成することです。ユーザーが値を要求すると、この格納された値 (向き == 'v' の場合) または(向き == 'h' の場合) を返すことができます。length_lengthlengthwidth

classdef MyObject
    properties (Dependent)
        length   % Can be either `width_` or `length_`
        width    % Can be either `width_` or `length_`
    end

    properties
        orientation
    end

    properties (Access = 'protected')
        length_ = 12
        width_ = 10
    end

    methods
        function mo = MyObject(o)
            mo.orientation = o;
        end

        function set.orientation(mo, newor)
            % If the user supplies an "o" flip the orientation
            if strcmpi(newor, 'o')
                if strcmpi(mo.orientation, 'v')
                    newor = 'h';
                else
                    newor = 'v';
                end
            end

            mo.orientation = newor;
        end    

        function set.length(mo, value)
            if strcmpi(mo.orientation, 'h')
                self.length_ = value;
            else
                self.width_ = value;
            end
        end

        function set.width(mo, value)
            if strcmpi(mo.orientation, 'h')
                self.width_ = value;
            else
                self.length_ = value;
            end
        end

        function res = get.width(mo)
            switch lower(mo.orientation)
                case 'h'
                    res = mo.width_;
                case 'v'
                    res = mo.length_;
                otherwise
                    error('Invalid orientation');
            end
        end

        function res = get.length(mo)
            switch lower(mo.orientation)
                case 'h'
                    res = mo.length_;
                case 'v'
                    res = mo.width_;
                otherwise
                    error('Invalid orientation');
            end
        end     
    end
end

lengthこの方法では、向きを変更したときに明示的に更新する必要はありません。

補足として、組み込みの function と少し混同されるため、プロパティとしては使用しませんlengthlength

于 2016-03-09T20:53:42.837 に答える