Clear Filters
Clear Filters

what is wrong of this class

1 view (last 30 days)
C Shen
C Shen on 23 Aug 2011
classdef dummya
properties
value;
end
methods
function obj = dummya( aa)
obj.value = test( aa);
end
end
methods (Static)
function y = my_pi(a)
y = 3.141592*test(a);
end
end
methods
function y = test(a)
y = 3.141592*a;
end
end
end

Accepted Answer

Jared
Jared on 23 Aug 2011
What is the error you are receiving? I saved your test code to file (dummy.m) and it seemed to work.
  3 Comments
Jared
Jared on 24 Aug 2011
Ok, I think the key is static methods.
Most class methods require an object as an input. That is, you could write:
function = test(obj,a)
...
end
You can call the method either as test(obj,a) or as obj.test(a) for some obj.
The exception to this are static methods. From the help file:
"Static methods are associated with a class, but not with specific instances of that class. These methods do not perform operations on individual objects of a class and, therefore, do not require an instance of the class as an input argument, like ordinary methods."
Thus, my_pi(a) works just fine, even though a is not an instance of the class, dummya.
Does this make sense?
C Shen
C Shen on 24 Aug 2011
But Static method can't call regular method, right?
classdef dummya
properties
value;
end
methods
function obj = dummya( aa)
obj.value = mtest(obj, aa);
obj.value = dummya.sfun(aa); %this does not work
end
end
methods (Static)
function obj = sfun(a)
obj = 2*mtest(obj,a);
end
end
methods
function y = mtest(obj, a)
y = 3*a;
end
end
end

Sign in to comment.

More Answers (0)

Categories

Find more on Construct and Work with Object Arrays in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

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

Start Hunting!