Property validation in subclass
7 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Deasy
am 4 Apr. 2017
Kommentiert: Guillaume
am 4 Apr. 2017
Hello everyone, i am new to the OOP in Matlab.
I have a superclass SimpleList with the property set, which is an array with any kind of data in it.
classdef SimpleList < handle
% SimpleList
properties
set(:,1)
end
methods
.
.
.
end
end
And a subclass ObjList, where the array should only contain objects of a specific class
classdef ObjList < SimpleList
% ObjectList
properties
end
methods
.
.
.
end
end
My Question is: How do i validate the property in ObjList?
Thanks in advance!
0 Kommentare
Akzeptierte Antwort
Guillaume
am 4 Apr. 2017
Note: I would recommeng giving a name other than set to your property. That name is already overloaded enough by matlab.
You can create a property validation function in the class that define the property using the syntax (for handle classes):
function set.propertyname(object, valuetoset)
%validation code
end
with your property name this would be
function set.set(this, value) %See why you should rename the property?
if ~strcmp(class(value), somevalidclass)
error('invalid class supplied to set');
end
this.set = value;
end
However, as I said, this property set method must be implemented in the class that defines the property, that is your base class, not the derived class. The workaround is from the set method to call another method that does the validation and is overridden by the derived class:
classdef SimpleList < handle
properties
set;
end
methods
function set.set(this, value)
isok = this.validateset(value);
if ~isok
error('invalid type of variable for set property)
end
this.set = value; %DO NOT set the value is a subfunction or you'll get infinite recursion
end
end
methods (Access = protected)
function isok = validateset(this, value)
isok = true;
end
end
end
classdef ObjList < SimpleList
methods (Access = protected)
function isok = validateset(this, value)
isok = strcmp(class(value), 'double');
end
end
end
0 Kommentare
Weitere Antworten (1)
Adam
am 4 Apr. 2017
Bearbeitet: Adam
am 4 Apr. 2017
In general I do this kind of thing via delegation from the superclass set function. Your property declaration confuses me though. Don't call a property 'set', it is asking for trouble. Also using (:,1) in the property block doesn't make much sense to me - is it even valid syntax there?
So to give an example:
classdef mySuperClass < handle
properties
myProp;
end
methods
function set.myProp( obj, myPropVal )
myPropVal = doValidateMyProp( obj, myPropVal );
obj.myProp = myPropVal;
end
end
methods( Access = protected )
function myPropVal = doValidateMyProp( obj, myPropVal )
end
end
end
classdef mySubClass < mySuperClass
methods( Access = protected )
function myPropVal = doValidateMyProp( obj, myPropVal )
% Do some validation
end
end
end
You don't have to return the value out of the function - it depends what type of validation you are doing. If you just want it to throw an error then get rid of the output type, but if, for example, you use validatestring the output argument can be useful.
I left the body of the doValidateMyProp function empty in the superclass - it is up to you what you put in here. Because I didn't make it abstract this will get called if your subclass does not override the function so it isn't compulsory for the subclass to do this.
1 Kommentar
Guillaume
am 4 Apr. 2017
Great minds think alike, or somesuch...
(:, 1) is indeed not valid. I assumed it was a typo.
I forgot to mention in my answer that if the base class is not meant to be used as a container on its own, then ideally it should be abstract.
Siehe auch
Kategorien
Mehr zu Properties 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!