Executing the constructor of a handle object with or without semicolon
Afficher commentaires plus anciens
Hi,
I'm seeing something that appears to me as very strange. I have handle class (called 'cube') which when I call the constructor like this:
a = cube(x,y,z,color,alpha);
Everything works as expected. However when I call the constructor methods without the semicolon, i.e.:
a = cube(x,y,z,color,alpha)
I find that the execution seems to continue pass the end of the constructor and run other functions in the methods section of my class. This is the class code...
classdef cube < handle
properties (GetAccess='public', SetAccess='private')
xIndex; % location of the cube in 3D space
yIndex;
zIndex;
end
properties (GetAccess='private', SetAccess='private')
graphicsHandle;
color;
alpha;
end
properties (Constant, GetAccess='private')
vertOffsets = [0 0 0; ... % cube vertices offsets configuration
1 0 0; ...
1 1 0; ...
0 1 0; ...
0 0 1; ...
1 0 1; ...
1 1 1; ...
0 1 1];
faces = [1 2 6 5; ... % cube faces configuration
2 3 7 6; ...
3 4 8 7; ...
1 4 8 5; ...
5 6 7 8; ...
1 2 3 4];
end
methods
function obj = cube(x, y, z, color, alpha) % constructor
obj.xIndex = x;
obj.yIndex = y;
obj.zIndex = z;
obj.graphicsHandle = [];
obj.color = color;
obj.alpha = alpha;
end;
function delete(obj) % destructor
if isempty(obj.graphicsHandle) == 0
delete(obj.graphicsHandle);
end
end;
function display(obj)
disp('displazing!');
dvert = obj.vertOffsets;
dvert(:,1) = dvert(:,1) + obj.xIndex; % translate vertices for drawing
dvert(:,2) = dvert(:,2) + obj.yIndex;
dvert(:,3) = dvert(:,3) + obj.zIndex;
% move the cube to the new location, creating the object if required
if isempty(obj.graphicsHandle)
obj.graphicsHandle = patch('Faces',obj.faces,'Vertices',dvert, ...
'FaceColor', obj.color, 'FaceAlpha', obj.alpha);
else
set(obj.graphicsHandle, 'Faces',obj.faces,'Vertices',dvert);
end;
end;
function move(obj, x, y, z)
obj.xIndex = x;
obj.yIndex = y;
obj.zIndex = z;
display(obj);
end;
end
end
Réponse acceptée
Plus de réponses (1)
Michael Allwright
le 17 Avr 2012
0 votes
Catégories
En savoir plus sur Construct and Work with Object Arrays dans Centre d'aide et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!