How to put condition (based on a property) in a property accessor

4 次查看(过去 30 天)
Hello,
I have a class property, which value is conditionned by another property of the class.
Let say I have a "Car" class with "brand" and "color" as property.
if the brand is Ferrari then the color can only be red or yellow, but is the brand is Mercedes, then color can only be Black or Silver.
The way I write it, matlab suggest me to put Color as dependent property. However color has to remain a user input, which seems not possible if it is set as dependent property...
Thanks in advance for your help.
  1 个评论
per isakson
per isakson 2017-10-25

请先登录,再进行评论。

回答(1 个)

Yair Altman
Yair Altman 2017-10-25
编辑:Yair Altman 2017-10-26
I suggest that you create a setter method for color that will check the relevant condition, for example:
function set.color(obj,value)
if strcmpi(obj.brand,'Ferrari')
if ~any(strcmpi(value,{'red','yellow'}))
error('bad_value', 'The color of Ferrari can only be red/yellow!')
end
elseif strcmpi(obj.brand,'Mercedes')
if ~any(strcmpi(value,{'black','silver'}))
error('bad_value', 'The color of Mercedes can only be black/silver!')
end
end
end
If you allow the user to modify the brand after the color was set, then you might want to add a corresponding setter method for the brand property as well.
  3 个评论
Adam
Adam 2017-10-26
编辑:Adam 2017-10-26
That error should link you to an explanation for that, but yes, you will need it to be a dependent property. Dependent properties are not only for getting purposes even though that is their intuitive use. You can use a dependent property with a set function also, but with a few caveats. In this case you need to use one because only a dependent property is allowed to have a set function that accesses other class properties. Or rather, that is recommended. It is a warning rather than an error, if I remember correctly. You can ignore it and the cases that it points to may never affect you, but ignoring warnings is not usually a good idea unless you are very well aware of what the warning is telling you and that it does not apply. Here it would just be bad practice to ignore it.
Mainly you need to use a second, private, variable to store the actual value. Usually I would create a colour_ variable for this, then in the set function like shown in the answer you would add
obj.colour_ = value;
at the end and you then also need to create a getter for your colour property as every dependent property must have a getter (though a setter is not compulsory and in normal usage is not desired, but in this case it is).
i.e.
function color = get.color( obj )
color = obj.colour_;
end
So in your properties you would have:
properties( Dependent, Access = public )
color;
end
properties( Access = private )
colour_;
end
Obviously you can feel free to spell your private variable in the non-English spelling too :)
You can read more about this in the Matlab OOP bible in section 8-59 and around there.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Scope Variables and Generate Names 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by