MATLAB OOP question.

3 ビュー (過去 30 日間)
Adam
Adam 2011 年 7 月 20 日
編集済み: per isakson 2020 年 9 月 4 日
OK possibly a silly question, be please read and help me out regardless. :)
Does an object have a sense of self?
What I mean is, let's say I want to have some method which you call that increases a protected property by 1.
So you call the method: MyObj.AddOneToThatThingyMaBob().
Inside the method, what do I use as a subsitute for "this" like you would use in C#? AKA in C# it might be something similar to: this.MyValue = this.MyValue + 1;
How do you do that in MATLAB?

採用された回答

Chirag Gupta
Chirag Gupta 2011 年 7 月 20 日
In MATLAB, a method would typically have a signature:
classdef test1234 < handle
properties (Access = public)
prop
end
methods
function obj = test1234()
obj.prop =0;
end
function AddOneToThatThing(obj)
obj.prop = obj.prop+1;
end
end
end
Then
a = test1234;
a.AddOneToThatThing();
a
Gives you what you need. The key here is Handle classes
  3 件のコメント
Adam
Adam 2011 年 7 月 21 日
OK this makes sense..."obj" is "this" when dealing with Handle classes!
Chirag Gupta
Chirag Gupta 2011 年 7 月 21 日
obj is just a variable name used typically in MATLAB (or by me)! If you prefer you could name it as 'this' instead of 'obj'

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCollect Decision, Condition, and MC/DC Coverage for MATLAB Code についてさらに検索

製品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by