Effacer les filtres
Effacer les filtres

How to get objects of different classes to interact

3 vues (au cours des 30 derniers jours)
David Davis
David Davis le 2 Mar 2017
I am currently working with two classes where one object is dependent of the other. Ex: lets say my classes are Bear () and Brush(). Both classes have defined properties and should be working fine but the issue that I have is when I want the bear to use a brush. Here is an example
bear = Bear()
paint = Brush()
redPaint = paint.setBrush('red',2);
bear.setBrush(redPaint);
.
all of this works and returns the following:
Bear with properties:
x: 0
y: 0
heading: 90
pen_on_paper: 1
pen: [1x1 Pen]
However the issue that I have is when I try to use the said pen. Ex:
bear.forward(2);
>>
bear.forward(2)
No public property color exists for class matlab.graphics.primitive.Line.
Error in Turtle/forward (line 38)
l.color = obj.pen;
here is the code for my method forward in the bear class
function obj = forward(obj,distance)
% move forward in current heading a set number of distance
x2 = obj.x + distance*cosd(obj.heading);
y2 = obj.y + distance*sind(obj.heading);
if obj.pen_on_paper
%draw a line
hold on
l = line([obj.x,x2],[obj.y, y2]);
l.color = obj.pen;
l.LineWidth = obj.pen;
hold off
pause(0.1)
end
% update location
obj.x = x2;
obj.y = y2;
end
This is what I don't understand. I can have my Bear hold any pen but I don't think that I am using the pen property inside of bear correctly because the color from the pen object that I make fails to be assumed by the lines.
Thoughts?

Réponses (1)

Steven Lord
Steven Lord le 2 Mar 2017
The primitive line object created by the line function does not have a property named 'color'. It has a property named 'Color'.
Before the introduction of the new graphics system in release R2014b, the way to change properties was through the set function and set was and is case insensitive.
myLineObject = line(1:10, 1:10);
set(myLineObject, 'color', 'r')
Unlike set, the dot notation introduced as part of the new graphics system is case sensitive.
myLineObject.color = 'c'; % throws an error
myLineObject.Color = 'c'; % works

Community Treasure Hunt

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

Start Hunting!

Translated by