Sharing variables between classes

9 次查看(过去 30 天)
Anastasia Vlasova
Anastasia Vlasova 2017-4-20
回答: Vaibhav 2024-7-25,6:21
Hello all,
I'm writing code to read and write a certain file format. Those are actually my first steps in OOP. The structure of the file is multilevel. First it has a header and tags. Tags might contain several objects inside. The tags as well as the objects inside the tags might have different structure and encoding. I have classes for each of those components: for the file itself, for each of the tag types, for the objects in tags. Each upper level contains the lower levels as parameters.
The problem that bothers me is that the upper level class has the parameters which the lower level classes also need. Like filename. In the first implementation I was just including filename as a parameter to each class, resulting at the end in multiple filename parameters inside the upper file class. It had an advantage though as I could address it inside any of the class methods as obj.filename.
The next try was to use it as an ordinary local variable, but then each of the class method got additional argument. The last step was to use it as a global variable. This basically does what it should and doesn't look too bad as for me, but I have often heard that global variable doesn't correspond to the concept of the OOP, it is not recommended to use it and so on.
So what is the right way to go? Just using persistent variable instead of global? Using handle classes or class inheritance...?
Thank you in advance!

回答(1 个)

Vaibhav
Vaibhav 2024-7-25,6:21
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!

类别

Help CenterFile Exchange 中查找有关 Methods 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by