starting with oop
조회 수: 1 (최근 30일)
이전 댓글 표시
Hi, I just started playing with oop in Matlab. Dont have any other oop experience, so I seem to get lost in the first steps alerady. when I try to call teh following class
classdef myclass
%MYCLASS Summary of this class goes here
% Detailed explanation goes here
properties
t1
end
methods
function obj=myclass(arg1)
obj.t1=myclass.p(arg1);
end
end
methods (Static)
function p = pi(tol)
[n d]=rat(pi,tol);
p=n/d;
end
end
end
I always get the error
The class myclass has no property or method named 'p'.
How can I prevent that error?
Cheers
댓글 수: 0
답변 (1개)
David Young
2011년 12월 1일
You need to replace
obj.t1=myclass.p(arg1);
with
obj.t1=myclass.pi(arg1);
You're making the (I suspect quite common) mistake of using the output variable name from the function instead of the name of the function itself.
If you make that change, the constructor runs:
>> m = myclass(3)
m =
myclass
Properties:
t1: 3
Does that do what you expect now?
댓글 수: 2
David Young
2011년 12월 1일
Yes, if pi is not static, you can do
obj.t1 = obj.pi(arg1);
By the way, it would be better to avoid "pi" as a method name, so that it can't be confused with the inbuilt function pi.
참고 항목
카테고리
Help Center 및 File Exchange에서 Software Development Tools에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!