Dependent property for subclasses
6 次查看(过去 30 天)
显示 更早的评论
Why does the following inheritance not work?
class A < handle
properties (Dependent)
a
end
methods
function this = A(a)
this.a = a;
end
function a = get.a(this)
a = this.a;
end
end
class B < A
methods
function this = B(a)
this.a = a;
end
function a = get.a(this)
a = this.a+1;
end
end
The error is:
The method get.a does not refer to a valid property name
0 个评论
回答(2 个)
Sai Veeramachaneni
2021-3-12
Hi,
get.a is not a valid function name, Valid function names begin with an alphabetic character, and can contain letters, numbers, or underscore only
Try changing it to getA instead of get.a
Hope it helps.
1 个评论
Steven Lord
2021-3-12
The first paragraph of your answer is true, but that syntax is valid in MATLAB. It is used to define a property accessor method for the a property. See this documentation page for more information.
Steven Lord
2021-3-12
You are only allowed to define a property accessor method for a property if
- the class in which the method is defined also defines the property
- the class does not include a properties block that defines that property but does allow dynamic properties (see the Set and Get Methods for Dynamic Properties documentation page linked in the See Also section), or
- the property is Abstract in its defining class, in which case a concrete subclass can define a property accessor method
No, a superclass defining a property in its properties block does not allow its subclasses to define a property accessor method. Doing so could allow the subclass to accept a value that would make the subclass instance not a valid superclass instance.
Consider a hierarchy where EquilateralTriangle inherits from Triangle. Triangle defines a property numSides and validates that this property always has value 3. [Yes, it should be a Constant property and not have a property accessor method, but this is a simplified example to illustrate a point.] If EquilateralTriangle could define a property accessor method that allows numSides to be 4 (due to a bug in the method) and you created an EquilateralTriangle with numSides equal to 4, it would not be a valid Triangle.
1 个评论
Captain Karnage
2023-4-20
One issue I'm having is if you use an Abstract property - you can't use validation functions in the property block. That gives the error:
Using size and validation functions on an inherited property is not supported.
What I want to do, is I'm creating a class with two integer values. I'm trying to make subclasses where the precision of the two values is different in each subclass - but they all share the same methods in the superclass so I only have to write the methods once (more importantly - only have to EDIT the methods once when I have to make changes). The idea is I have a superclass, let's call it DInteger.. and I want subclasses DInt, DInt8, DInt16, DInt32, and Dint64. The two properties are I1 and I2 (and also a logical named sign).
If I could do what I wanted, the superclass, DInteger would validate using {mustBeInteger} which will allow any integer type or a floating type that is equal to an integer - then DInt would set the type of I1 and I2 as double, DInt8 would set them to uint8, up to Dint64 which would set them to uint64. I can use whatever validators in the superclass, but can't add any validators in the subclass. I can use validation in my constructors and other functions that set the properties, but that doesn't guarantee the property doesn't get set to a wrong type somewhere. Is there anyway to do that?
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Subclass Definition 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!