Set properties from Superclass in subclass
27 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
elevonm
am 20 Aug. 2021
Kommentiert: Turlough Hughes
am 20 Aug. 2021
Hello, I have defined a superclass 'food.m' that has certain properties:
classdef food
properties
vitamins
protein
carbs
end
end
Now I want to define a subclass that inherits from the above, and also sets the vitamins property (in other words, I will want all instances of this subclass to have the property 'Vitamins' set to "C")
classdef fruit < food
properties (Access = private)
vitamins = "C"
end
end
The following error is raised
%Cannot define property 'vitamins' in class 'fruit' because the property has already been defined in the
%superclass 'food'.
How do I fix this?
Thank you in advance for your help!
0 Kommentare
Akzeptierte Antwort
Turlough Hughes
am 20 Aug. 2021
Bearbeitet: Turlough Hughes
am 20 Aug. 2021
Set the Access attribute in the superclass:
classdef food
properties
protein
carbs
end
properties (Access = private)
vitamins
end
end
otherwise, the default is that vitamins is defined as a public property in the superclass.
8 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Class Introspection and Metadata 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!