In Matlab (2017b) I'm trying to implement a superclass with a yet undefined abstract property.
classdef Class_Test1
properties (Abstract=true)
obj
end
end
Subclasses should then implement this property and restrict it to a certain class, e.g.:
classdef Class_Test2 < Class_Test1
properties
obj#double = 123;
end
end
a = Class_Test2; now throws an error:
The property 'obj' restriction defined in class 'Class_Test2' must match
the property definition in base class 'Class_Test1'.
Is there no way to specify the class / type of obj in the implementation of the abstract class or am I missing something?
In the end I would like obj to be abstract in Class_Test1 to implement several subclasses, which will hold objects of different classes in obj.
Any help appreciated ...
MATLAB classes are weird... or in any case very different from what you expect from a class if you've learnt about classes in any other programming language.
One aspect of MATLAB is that it's an interpreted language. Stuff is evaluated when executed, not when compiled. So code doesn't always need to make sense.
So, we can solve your issue like this:
classdef (Abstract) foo
methods
function c = getclass(in)
c = class(in.obj);
end
end
end
The function foo.getclass seems to not make sense, since foo doesn't have a property obj. But because foo is abstract, we're never going to have an object of class foo on which we'll call the getclass method. Whenever getclass is called, it'll be a derived class. So, if we want to use this getclass method, we need to make sure that the derived class has a property obj:
classdef foo_uint8 < foo
properties
obj#uint8 = uint8(1);
end
end
It is now possible to call foo_uint8.getclass:
>> a = foo_uint8;
>> a.getclass
ans =
'uint8'
OK, so after looking into this further it seems that this is a deliberate limitation based on the design of Matlab. I'm not too familiar with OOP in other languages but would be interested to learn if this is an general OOP fundamental or rather Matlab specific.
As a workaround I have come up with the following solution:
classdef (Abstract) Class_Test1 < handle
properties
Obj
end
properties (Abstract = true, Constant)
ObjClass#char
end
methods
function set.Obj(obj_in,in)
if isa(in, obj.ObjClass)
obj_in.Obj = in;
end
end
end
end
... and the implementation ...
classdef Class_Test2 < Class_Test1
properties (Constant)
ObjClass#char = 'double'; % Define the allowed class here ...
end
end
I would still be interested to learn about other possible solutions.
Related
I have a superclass
classdef superclass
properties
a
b
methods
% constructor
function obj = superclass(a,b)
obj.a = a;
obj.b = b;
end
... some code...
function c = plus(obj1,obj2)
c = superclass(obj1.a+obj2.b,obj1.b)
end
(There is a lot of type checking and error throwing but this is the gist of it). This class has almost all of the operators are overloaded like this. Those which are not, throw an error. Now I have derived another class from this which adds some properties.
classdef subclass < superclass
properties
p
q
methods
... constructor and such...
end
Now when I do any operation on this class like adding it will return an object of the superclass (because of my overloaded plus function). The only way I see to fix this is overload all of those functions once again. That is tedious, is there a better way?
First of all, I belive I should say that I am a newbie on OOP in Matlab, but quite experenced with other languages like C++. So this question may sound like so easy to you but I somehow could not find an explanation to my problem.
Suppose that I have two classes, where the former one is an Abstract class and the latter one is a derived class which is derived from the abstract base class.
You can see an example below.
classdef (Abstract) queue < handle
properties
queue_array = [];
read_index =1;
end
methods
function insert_to_queue (obj, queue_element)
obj.queue_array = [obj.queue_array, queue_element];
end
function clean_queue (obj)
obj.queue_array = [];
obj.read_index = 1;
end
end
end
classdef fifo_queue < queue
properties
end
methods
function get_next_from_queue (obj)
...
end
end
end
Here as you can see, I put the common functions of all the derived classes to the base class for the purpose of code reuse.
However when I try a scrpt like below
f_que = fifo_queue;
f_que.instert_to_queue(some_data); % OK
f_que.clear_queue(); % Error
I got an error like;
No appropriate method, property, or field print for class fifo_queue
Do you have any idea of why I face with this type of behaviour on MATLAB?
Thanks
I generally find the set/get interface defined in hgsetget very useful for setting and getting multiple parameters at the same time. I recently found it was especially suited for object construction.
Example:
classdef testclass < hgsetget
properties
A
B
C
D
end
methods
function obj = testclass(varargin)
if ~isempty(varargin)
set(obj,varargin{:})
end
end
end
end
Usage:
>> a = testclass('A',1,'B',2)
a =
testclass handle
Properties:
A: 1
B: 2
C: []
D: []
Despite the slowness of this interface I'm really happy of the flexibility it provides.
What is more annoying for my application is that I obtain a handle class (by inheritance from hgsetget).
To circumvent this my first guess was to construct an abstract class with my set/get definition inside. Very simply written it gives:
classdef (Abstract) myAbstractClass
methods
function obj = set(obj,varargin)
for i = 1:2:length(varargin)
obj.(varargin{i}) = varargin{i+1};
end
end
function val = get(obj,varargin)
val = cell(length(varargin),1);
for i = 1:length(varargin)
val{i} = obj.(varargin{i});
end
end
end
end
and then set it as superclass for my test class
classdef testclass < myAbstractClass
properties
A
B
C
D
end
methods
function obj = testclass(varargin)
if ~isempty(varargin)
set(obj,varargin{:})
end
end
end
end
However I must misunderstand something in the construction mechanism because here is what happens:
>> a = testclass('A',1,'B',2)
ans =
testclass
Properties:
A: 1
B: 2
C: []
D: []
Methods, Superclasses
a =
testclass
Properties:
A: []
B: []
C: []
D: []
Methods, Superclasses
If somebody knows the reason of this behavior, I'm totally open to her/his explanations
Thank you in advance
JM
Overall your design looks good, but within your constructor method testclass, you need to write
obj = set(obj,varargin{:});
Because you are using a value class rather than a handle class, you need to retrieve the output of your set method, otherwise you'll have set the property of something, and then discarded it and returned a completely different (and empty) obj as output.
Also, you need to add a semi-colon after the call to set; otherwise it will display the intermediate something.
You may also want to revise your opinion of handle classes; they're really very natural to use (just my opinion, but more natural in an OO context than value-behaviour).
I'd like to customize the display of an enumeration class using matlab.mixin.CustomDisplay.
If I have a regular (non-enumeration) class such as the following:
classdef test < handle & matlab.mixin.CustomDisplay
properties
value
end
methods
function obj = test(value)
obj.value = value;
end
end
methods (Access = protected)
function displayScalarObject(obj)
disp(['hello ', num2str(obj.value)])
end
end
end
then everything works fine - for example,
>> a = test(1)
a =
hello 1
But if I have an enumeration class such as the following (note the addition of the enumeration block):
classdef test < handle & matlab.mixin.CustomDisplay
properties
value
end
methods
function obj = test(value)
obj.value = value;
end
end
methods (Access = protected)
function displayScalarObject(obj)
disp(['hello ', num2str(obj.value)])
end
end
enumeration
enum1(1)
end
end
then the display is not customized - for example,
>> a = test.enum1
a =
enum1
Using the debugger, I can see that my displayScalarObject method is never called. Implementing other methods of matlab.mixin.CustomDisplay such as displayNonScalarObject and so on doesn't seem to help - these never get called either.
What's going on? Do disp and display work differently for enumeration classes, in such a way that anything that's overridden by matlab.mixin.CustomDisplay just gets ignored?
Is there a way to get a customized display with matlab.mixin.CustomDisplay, but using an enumeration class?
PS I am able to directly overload disp and/or display on an enumeration class that does not inherit from matlab.mixin.CustomDisplay, and this works fine. But I'm looking to use matlab.mixin.CustomDisplay if possible.
I'll answer my own question in case anyone else is interested.
The question was asked with reference to MATLAB R2014a. In R2014b, an additional note has been added to the documentation for matlab.mixin.CustomDisplay, making it explicit that it is not possible to use matlab.mixin.CustomDisplay to derive a custom display for enumeration classes, and suggesting that the best approach is to overload disp.
I'm starting out with object-oriented programming in MATLAB, and I'm confused on how to best pass objects to other objects, as MATLAB doesn't feature static type definitions.
I have three different classes, all of which include some constants. Now, I want to use the constants defined in two of the classes in the methods of the third class - how should I do this? The classes are in no hierarchy.
So, I'm looking for something like #include in C++.
Problem illustrated below. How to write "*Object1" and "*Object2" references to access const1 and const2?
classdef Object1
properties (Constant)
const1 = 100;
end
methods
function Obj1 = Object1()
end
end
classdef Object2
properties (Constant)
const2 = 200;
end
methods
function Obj2 = Object2()
end
end
classdef Object3
properties (Immutable)
property3
end
methods
function Obj3 = Object3()
Obj3.property3 = *Object1.const1 + *Object2.const2;
end
end
Just remove the asterisks, and I think you have what you need.
There are a couple of other syntax errors in your code (replace Immutable with SetAccess = immutable, and add missing ends to the classdefs), but once I made those changes, I get:
a = Object3
a =
Object3
Properties:
property3: 300
Methods
In general, to reference a Constant property from another class, just prefix the property with the class name (and possibly package name, if the classes are in a package).