필터 지우기
필터 지우기

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