フィルターのクリア

Organization of Object Oriented Code

5 ビュー (過去 30 日間)
Alex
Alex 2023 年 5 月 16 日
回答済み: FannoFlow 2023 年 5 月 17 日
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 件のコメント
Jeff Miller
Jeff Miller 2023 年 5 月 17 日
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 日
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

カテゴリ

Help Center および File ExchangeEnvironment and Settings についてさらに検索

タグ

製品


リリース

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by