Borrar filtros
Borrar filtros

what is wrong of this class

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

Respuesta aceptada

Jared
Jared el 23 de Ag. de 2011
What is the error you are receiving? I saved your test code to file (dummy.m) and it seemed to work.
  3 comentarios
Jared
Jared el 24 de Ag. de 2011
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 el 24 de Ag. de 2011
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

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Construct and Work with Object Arrays en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by