Organization of Object Oriented Code

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

Don't you need this?
classdef(Abstract = true) foo % adding "= true"
Alex
Alex 2023년 5월 17일
perhaps, but I stil get the same error with = true

댓글을 달려면 로그인하십시오.

답변 (1개)

FannoFlow
FannoFlow 2023년 5월 17일

0 개 추천

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

카테고리

도움말 센터File Exchange에서 App Building에 대해 자세히 알아보기

제품

릴리스

R2023a

태그

질문:

2023년 5월 16일

답변:

2023년 5월 17일

Community Treasure Hunt

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

Start Hunting!

Translated by