Create Class that is both Heterogeneous and a Handle class

4 次查看(过去 30 天)
2 things.
1) I am looking to create a class that is both a handle class object which allows reference copies of that first object to be distributed / moved which may edit the contents of the first class object AND also be "matlab.mixin.Heterogeneous" which allows there to be an array of objects that differ in their specific class, but are all derived from one root class. In this case that I am looking for all these objects root and derived to also be handel classes.
2) I'm looking for some way to add/remove oject handles from an object's properties dynamically. I'm not sure how this is normally handeled is Object Oriented Programming, where objects are moved between other objects like people moving form one location to another location.
For example, if I were to simulate people at a obstical course, I would have a class called People, and a root class called Locations for which there are derived classes from, like Swing, Slide, RockWall exe. In this example I want the people object to have a handle to their current location (object), and the different location classes to have a handle to what person is persent, and what location is next in the obstical course after the current one.
Alternativly, if there is maybe a better way of structuring this then I've mentioned I would like to know.
classdef Location < matlab.mixin.Heterogeneous %< handle ??????????? NOT SURE HOW MAKE THIS A HANDLE
% "Location" objects are able to change properties of "Person" obj and
% "Location" obj through the handles stored in this classes local properties
properties
locationID (1,1) uint16 {mustBeInteger,mustBePositive} = 1
person (1,1) Person %handle ref, so this class may make changes to it
%next location in the obstical course:
nextLocation (1,1) Location %handle ref, so this class may make changes to it
end
methods
function movePerson(NewLocation,Person)
%find person's prev location
prevLocation = Person.currentLocation;
%check if new Location's person is empty
if isempty(NewLocation.person)
%set person's prev location to empty
clear(prevLocation.person);%????????? NOT SURE HOW TO DO THIS
%Update Location's person property
NewLocation.person = Person; %copy person handle to location obj
%Update Person's Location property
Person.currentLocation = NewLocation; %copy location handel to person obj
end
end
end
end
classdef Swing < Location %< handle
methods
function done = sendNextLocation(Location)
if isempty(Location.nextLocation.person) %Swing objects may only accept one person at at a time
Location.AddPerson(Location.nextLocation,Location.person);
Location.RemovePerson(Location);
done = 1;
else
done = 0;
end
end
end
end
classdef Person < handle
properties
ID (1,1) uint16 {mustBeInteger,mustBePositive} = 1
currentLocation (1,1) Location
end
end

采纳的回答

Steven Lord
Steven Lord 2024-4-22
I am looking to create a class that is both a handle class object which allows reference copies of that first object to be distributed / moved which may edit the contents of the first class object AND also be "matlab.mixin.Heterogeneous"
Sure. You just need to define your class to inherit from both handle and matlab.mixin.Heterogeneous. I've commented out the syntax for doing so below because I want to run code later in this Answers post. Note the ampersand (&) between the two base classes.
% classdef myclass < handle & matlab.mixin.Heterogeneous
A number of classes in MATLAB already do this, like the Handle Graphics classes.
f = figure;
isa(f, 'handle')
ans = logical
1
isa(f, 'matlab.mixin.Heterogeneous')
ans = logical
1
I'm looking for some way to add/remove oject handles from an object's properties dynamically.
So not adding/removing properties (which would involve using the dynamicprops Class) but changing the values of those properties? That's basic assignment.
For example, if I were to simulate people at a obstical course, I would have a class called People, and a root class called Locations for which there are derived classes from, like Swing, Slide, RockWall exe. In this example I want the people object to have a handle to their current location (object), and the different location classes to have a handle to what person is persent, and what location is next in the obstical course after the current one.
With this class hierarchy, as long as you have access to the handle of the Location to which the Person is going to move, you could simply temporarily remember in a local variable the Person's whereAmI property, update the Person's whereAmI property to the new Location handle, then update (using the local variable and the new Location variable) both the old Location's whoIsHere property (to remove the Person) and the new Location's whoIsHere property (to add the person.) It might look something like:
function moveToNewLocation(personObj, newLocationObj)
oldLocationObj = personObj.whereAmI;
addPersonToLocation(newLocationObj, personObj); % Method of Location
personObj.whereAmI = newLocationObj;
removePersonFromLocation(oldLocationObj, personObj); % Method of Location
end
Error handling (what happens if the new location has too many people?) is left to you.
  2 个评论
Cody Brown
Cody Brown 2024-4-22
That helps alot. I do still have a question regarding how to handle, in this example, a location not having any person present. This is where I thaught the property would have to be dynamic, although I don't know if this is good practice. Being that the person prpoperty in the location class, which holds a person object itself, can't be empty (or can it?) would the solution be to create a person object that represents no person at all as a filler that is used to fill locations where no person is occupying?? is making the location class < dynamicprops be the ideal solution?
This would have to be addressed in the Location's method:
removePersonFromLocation(oldLocationObj, personObj)
Steven Lord
Steven Lord 2024-4-22
You can have an empty array of handle objects. The gobjects function creates an array suitable for holding all sorts of Handle Graphics objects, so it's convenient to use for this example, but you could use anything that creates a handle object instead. [It may not have the same syntax as the gobjects function, though.]
x = gobjects(1, 0)
x =
1x0 empty GraphicsPlaceholder array.
isa(x, 'handle')
ans = logical
1
isempty(x)
ans = logical
1

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Graphics Object Programming 的更多信息

标签

产品


版本

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by