Class method not working properly

10 vues (au cours des 30 derniers jours)
James
James le 23 Sep 2011
Hello,
My method function "move(obj)" does nothing when it is called. The obj.pos is left unchanged. Can anyone tell me what I'm doing wrong? Here's the class code:
classdef ball
properties
pos
vel
rad
end
methods
function obj = ball(pos,vel,rad)
if nargin ~= 0
obj=ball;
obj.pos = pos;
obj.vel = vel;
obj.rad = rad;
end
end
function draw(obj) (...)
end
function move(obj)
obj.pos(1)=obj.pos(1)+obj.vel(1);
obj.pos(2)=obj.pos(2)+obj.vel(2);
end
end
end
Thanks!
-Jim

Réponse acceptée

Daniel Shub
Daniel Shub le 23 Sep 2011
Your class is a value class. If you want the new value of obj, then you need the move method to return obj. You may want your class to be a handle class
classdef ball < handle
  1 commentaire
James
James le 23 Sep 2011
Thanks!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Class File Organization dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by