Organization of Object Oriented Code
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Appologies if this is already answered somwhere ...
I'm trying to create an organizational structure fo my object oriented code. I think the best way to do it is with a combination of packages and classes. Currently, I have a folder structure something like this :
+MyPackage
+Foo
@bar
Inside +Foo I have a class definition for Foo. My inention is for this to be an abstract class that currently has no properties and only a constructor methods
classdef(Abstract) foo
%FOO Summary of this class goes here
methods(Abstract)
function obj = foo()
obj = [];
end
end
end
Inside @bar I have a class definition with a single property defined with a contructor method for bar. I would like to have other methods be .m files in the @bar folder
classdef bar < foo
%BAR Summary of this class goes here
properties
data
end
methods
function obj = bar(data)
obj = data;
end
end
end
My hope is to create an instance of foo by doing something like
obj = MyPackage.foo.bar(data)
Then access the not-yet-written methods in the @bar folder using something like
obj.modify_bar()
Howver, currently when I try to create a instance of bar I get the following error :
Error using MyPackage.foo.bar
The specified superclass 'foo' contains a parse error, cannot be found on MATLAB's search path, or is shadowed by another file with the same name.
foo appears ot the be on the matlab path because it's a package folder.
Am I do something fundementally worng? Should I be structuing my code in a different fashion? Sorry I'm new of object oriented programming in matlab.
2 Kommentare
Antworten (1)
FannoFlow
am 17 Mai 2023
Here is how I would recommend you create the folder structure:
+Package/+Foo/@Foo/Foo.m
+Package/+Foo/@Bar/Bar.m
Foo.m:
classdef (Abstract=true) Foo
end
Bar.m:
classdef Bar < Package.Foo.Foo
%BAR Summary of this class goes here
properties
data
end
methods
function obj = Bar(data)
obj = data;
end
end
end
And finally:
myBar = Package.Foo.Bar(42);
myBar.data
0 Kommentare
Siehe auch
Kategorien
Mehr zu Environment and Settings finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!