Set properties from Superclass in subclass

조회 수: 27 (최근 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 CenterFile Exchange에서 Food Sciences에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by