Hi Anastasia
One approach is to use class properties and inheritance to manage shared parameters like filename. You can create a base class that contains the shared properties and then inherit from this base class in your other classes.
classdef BaseFile
properties
filename
end
methods
function obj = BaseFile(filename)
obj.filename = filename;
end
end
end
classdef Tag < BaseFile
properties
tagData
end
methods
function obj = Tag(filename, tagData)
obj@BaseFile(filename);
obj.tagData = tagData;
end
end
end
classdef File < BaseFile
properties
tags
end
methods
function obj = File(filename, tags)
obj@BaseFile(filename);
obj.tags = tags;
end
end
end
In above example, BaseFile is a base class that holds the filename property. Both Tag and File classes inherit from BaseFile, so they also have access to the filename property.
If you need to modify shared properties and have those changes reflected across all instances, you might want to use handle classes. Handle classes allow you to create references to objects rather than copies.
classdef BaseFileHandle < handle
properties
filename
end
methods
function obj = BaseFileHandle(filename)
obj.filename = filename;
end
end
end
classdef TagHandle < BaseFileHandle
properties
tagData
end
methods
function obj = TagHandle(filename, tagData)
obj@BaseFileHandle(filename);
obj.tagData = tagData;
end
end
end
classdef FileHandle < BaseFileHandle
properties
tags
end
methods
function obj = FileHandle(filename, tags)
obj@BaseFileHandle(filename);
obj.tags = tags;
end
end
end
Using global variables is generally discouraged in OOP because it can lead to code that is difficult to understand and maintain. Instead, consider passing shared parameters through constructors and methods.
Hope this gets you started!