2

以下のクラスを受講

classdef MyClass
    properties (Access = public)
        MyProperty;
    end

    methods
        function this = MyClass()
            % initialize the class
            this.MyProperty = [];
            this.LoadProperty(2,2);
        end

        function p = GetProperty(this)
            p = this.MyProperty;
        end

        function this = LoadProperty(this, m, n)
            % loads the property
            this.MyProperty = zeros(m, n);
        end
    end
end

そして、次のように呼び出します。

obj = MyClass();
obj.GetProperty()

返されます-- これは、コンストラクター メソッドで[]割り当てられた最初の値です。MyProperty

メソッドはセッターとして機能していますLoadPropertyが、何も設定しません。のセッターを作成するにはどうすればよいMyPropertyですか? 私は C# のバックグラウンドを持っていますが、とても簡単です。-> 解決済み (下記参照)

C# のように参照のみを送信するのではなく、MATLAB は常にオブジェクト自体をクラスのすべてのメソッドに最初のパラメーターとして送信するため、参照とオブジェクトの問題であると思われます。

前もって感謝します!


編集:

this.LoadProperty(2,2);行をに変更するとthis = this.LoadProperty(2,2);、機能します。

C#、C++、Java などで通常期待されるように、クラス プロパティのみを設定する void-return メソッドを MATLAB で作成する方法はありますか?

4

2 に答える 2

4

クラス定義 (最初の行) を次のように変更します。

classdef MyClass < handle

handleこれは、クラスから継承することにより、参照セマンティクスを持つクラスを定義します。ドキュメントは違いを説明しています:

于 2013-09-10T01:10:01.823 に答える
2

パブリックとして宣言しているため、直接アクセスできます。

classObj = MyClass; 
classObj.MyProperty = 20;   
classObj.MyProperty % ans = 20

しかし、それをカプセル化したいようです。いくつかの方法があります。次のように、プライベートアクセスを使用しているとします。

classdef MyClass
    properties (Access = private)
        MyProperty;
    end

    methods
        function this = MyClass()
            % initialize the class
            this.MyProperty = [];
            this.LoadProperty(2,2);
        end

        function p = GetProperty(this)
            p = this.MyProperty;
        end

        function this = LoadProperty(this, m, n)
            % loads the property
            this.MyProperty = zeros(m, n);
        end
    end 
end

次に、次のように set メソッドを追加できます (通常、関数と変数には小文字を使用し、クラスには大文字を使用します。必要に応じて大文字に変更できます)。

function this = setProperty(this,value)
   this.MyProperty = value;
end

これはハンドル クラスではないため、この関数を次のように使用する必要があります。

myClass = myClass.setProperty(30); % You can also set it to [30 30 30; 20 20 20] if you want, there are no restrictions if you don't explicitly write into your function.

それ以外の場合は、次のようにしてハンドル クラスを使用できます。

classdef MyClass < handle

この場合、次のようにして直接変更できます。

myClass.setProperty(40);

ただし、これは、このクラスへの参照は新しいオブジェクトを作成せず、このオブジェクトからの別のハンドルになることも意味します。つまり、次の場合です。

myClass2 = myClass;
% and uses myClass2.setProperty:
myClass2.setProperty(40)
myClass.GetProperty % ans = 40!

したがって、この種の動作を回避したい場合 (つまり、クラスを関数または別の変数に渡すときにクラスのコピーが必要な場合、別名、値による呼び出し)、get および set メソッドの方法を指定したい場合Matlab は、プロパティを割り当てるときにオーバーロードできる 2 つの組み込みメソッドを提供します。あれは:

function out = get.MyProperty(this)
function set.MyProperty(this,value)

これらのメソッドを上書きすることで、ユーザーが呼び出したときに何が起こるかを上書きしています

myClass.MyProperty          % calls out = get.MyPropertyGet(this)
myClass.MyProperty = value; % calls set.MyProperty(this,value)

ただし、ハンドル クラスを操作して、クラスのコピー関数を作成することもできます。

function thisCopy = copy(this)
   nObj = numel(this);
   thisCopy(nObj) = MyClass;
   meta = metaclass(MyClass);
   nProp = numel(meta,'PropertyList');
   for k = 1:nObj
     thisCopy(k) = MyClass; % Force object constructor call
     for curPropIdx=1:nProp
       curProp = meta.PropertyList(curPropIdx);
       if curProp.Dependent
         continue;
      end
      propName = curProp.Name;
      thisCopy(k).(propName) = this(k).(propName);
    end
  end
end

これはget. set.、classdef 内で public メソッドとして (メソッドと同様に) 指定する必要があります。このメソッドを宣言してclass2、 のコピーにしたい場合は、次のclassようにします。

myClass = MyClass;
myClass.setProperty(30);
myClass2 = copy(myClass);
myClass2.setProperty(40); %
myClass.GetProperty % ans = 30

handleクラスオブジェクトからすべての(非)プロパティをコピーし、クラスオブジェクト配列があるときに機能するため、 MyClass にする必要があるのはもう少し複雑です。詳細については、@Amro の回答matlab oop のドキュメントを参照してください。

これは、機能する理由と機能しない理由の説明でもありthis = this.LoadPropertyますthis.LoadProperty(2,2)

于 2013-09-10T01:26:28.890 に答える