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일

2 개 추천

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

Chirag Gupta
Chirag Gupta 2011년 7월 20일
편집: per isakson 2020년 9월 4일
You could do this with Value classes as well:
classdef test1234
properties (Access = public)
prop
end
methods
function obj = test1234()
obj.prop = 0;
end
function obj = AddOneToThatThing(obj)
obj.prop = obj.prop+1;
end
end
end
But then you will have to always return the modified obj back.
a = test1234;
a = a.AddOneToThatThing() % Reassign it
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개)

카테고리

도움말 센터File Exchange에서 Collect Decision, Condition, and MC/DC Coverage for MATLAB Code에 대해 자세히 알아보기

제품

질문:

2011년 7월 20일

편집:

2020년 9월 4일

Community Treasure Hunt

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

Start Hunting!

Translated by