フィルターのクリア

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 Center および File ExchangeConstruct 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