Set properties from Superclass in subclass

28 ビュー (過去 30 日間)
elevonm
elevonm 2021 年 8 月 20 日
コメント済み: Turlough Hughes 2021 年 8 月 20 日
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!

採用された回答

Turlough Hughes
Turlough Hughes 2021 年 8 月 20 日
編集済み: Turlough Hughes 2021 年 8 月 20 日
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.
You can get a list of default property attributes here.
  8 件のコメント
elevonm
elevonm 2021 年 8 月 20 日
Thank you very much for your help!
Turlough Hughes
Turlough Hughes 2021 年 8 月 20 日
Happy to help :)

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeFood Sciences についてさらに検索

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by