Borrar filtros
Borrar filtros

starting with oop

1 visualización (últimos 30 días)
Christof
Christof el 1 de Dic. de 2011
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

Respuestas (1)

David Young
David Young el 1 de Dic. de 2011
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 comentarios
Christof
Christof el 1 de Dic. de 2011
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 el 1 de Dic. de 2011
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.

Iniciar sesión para comentar.

Categorías

Más información sobre Scope Variables and Generate Names en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by