How to use property from one class in to define a property in another class?

조회 수: 19 (최근 30일)
Hi All,
I have defined one class
classdef AgentVars
properties
set_o = linspace(-0.2,0.2,20) % set of observations
end
end
I want to use the the set_o property in another class. For example
classdef Agent
properties
set_o = AgentVars.set_o
end
end
However, currently I am not able to access a property from the other class. How can I do that? Thank you

채택된 답변

Rik
Rik 2021년 8월 6일
I don't know what the 'correct' answer would be, but you can also set the value in the constructor:
classdef Agent
properties
set_o = NaN
end
methods
function obj=Agent
obj.set_o=AgentVars.set_o
end
end
end
You could also inherit from a superclass, but that only works if you want to inherit from a single class.
  댓글 수: 8
Rik
Rik 2021년 8월 6일
As far as I know that isn't an official term. Take a look at the file I attached: the values of some properties are calculated based on the values of other properties. Because those aren't yet initialized, they can't be accessed in the properties block. That is why they are calculated in the constructor.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Construct and Work with Object Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by