Can multiple classes share external methods without inheritance?
Mostrar comentarios más antiguos
I am trying to explore OOP capabilities in MATLAB for a project I am working on. The idea is that I want to have a project class and within that, subclasses for each data type. For example:
classdef myproject < handle
properties (SetAccess = public, GetAccess = public)
d1
d2
d3
end
methods (Access = public) % constructor
function self = myproject
d1 = data_class1;
d2 = data_class2;
d3 = data_class3;
end
end % end constructor
% Public methods
methods (Access = public)
function myproject_filter(self)
% external filter here
% can
end
end % end public methods
end % end myproject class
%%%%%%%%%%%%%%%%% NEW CLASS %%%%%%%%%%%%%%%%
classdef data_class1 < handle %?? - not a subclass, right?
properties (SetAccess = public, GetAccess = public)
data
samplingrate
end
methods (Access = public) % constructor
function self = myproject
self.data = load("data_class1_data.mat") % some file
self.samplingrate = 1e500000; %Hz - absurd sampling rate
end
end % end constructor
% Public methods
methods (Access = public)
% generic method
function myproject_filter(self,filt_band)
% external filter
localdata = external_filter(self.d1.data,.self.samplingrate,filt_band);
self.data = localdata;
end
% class specific method
function dataclass1_method1(self)
% method specific to dataclass1
end
end % end public methods
end % end data_class1
The idea is to have a project class that helps manage overall project information and data classes. There will be some methods in the project class and the data_class classes that are shared. For example, time domain filtering can be generic, and shared across multiple data_class classes. However, there are some methods that are specific to each data_class. Is it possible to have external methods that multiple classes share? I am familiar with using external files for methods by placing myproject.m in a @myproject directory. However, as expected, if I try to add another class to that folder, there is an error that the directory and class name to not agree. The only way I can currently think to implement someting like this is to have the methods in each data_class and myproject call some external static methods.
I dont know if my data_class can inheret only some of the methods from the myproject class or if I make data_class < myproject, it will inheret all the methods and properties.
I may be in horrible violation of basic OOP principles, so please let me know if this is just a major "no-no". Please let me know if this question isn't clear.
Thanks!
Respuesta aceptada
Más respuestas (1)
Is it possible to have external methods that multiple classes share?
It sounds like the "external methods" you are trying to implement needn't be class methods at all. Why not just make them ordinary Matlab functions with global scope?
Another option would be to put all project and data classdefs in some common parent folder and all functions you wish them to share in a private/ subfolder. Any functions in a private/ subfolder will be seen only by code in the parent folder.
1 comentario
Justin Brantley
el 19 de Ag. de 2019
Categorías
Más información sobre Handle Classes en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!