필터 지우기
필터 지우기

starting with oop

조회 수: 1 (최근 30일)
Christof
Christof 2011년 12월 1일
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

답변 (1개)

David Young
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
Christof
Christof 2011년 12월 1일
that was the mistake. thanks.
another question, though. if I define the method as a regular method, not as a static method, I cant call it from the constructor. is there a way to do?
David Young
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 CenterFile Exchange에서 Scope Variables and Generate Names에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by