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

조회 수: 4 (최근 30일)
Laurent Davenne
Laurent Davenne 2017년 10월 20일
댓글: Laurent Davenne 2017년 12월 1일
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에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by