what is wrong of this class

1 次查看(过去 30 天)
C Shen
C Shen 2011-8-23
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

采纳的回答

Jared
Jared 2011-8-23
What is the error you are receiving? I saved your test code to file (dummy.m) and it seemed to work.
  3 个评论
Jared
Jared 2011-8-24
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 2011-8-24
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

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Construct and Work with Object Arrays 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by