How to use properties of a class in its own class and also in other classes?
7 views (last 30 days)
Show older comments
Hi guys,
I want to use parameters of one class in another class. I cannot use superclass and subclass. I am explaining you with an example.
classdef clCore
properties
mc
lcc
end
properties(Dependent)
Acbl
Albl
end
methods
function objCore=clCore(mc,lcc)
objCore.mc=mc;
objCore.lcc=lcc;
end
function Acbl=get.Acbl(objCore)
Acbl=objCore.mc+objWP.wpw
end
end
end
classdef clPrimary
properties
wpw
dpw
end
properties(Dependent)
dpu
wpu
end
methods
function objWP=clPrimary(wpw,dpw)
objWP.wpw=wpw;
objWP.dpw=dpw;
end
function dpu=get.dpu(objWP)
dpu=objWP.wpw+objCore.lcc
end
end
end
Here my question is i have defined two classes inorder to calculate the parameters in my function which are dependend on each other, how to use the properties of one class in another class. This is just a part of my code. I have to use the properties of clCore in clPrimary and also clPrimary in clCore. I dont understand how to do that. Could anyone help me out with this problem?
Thankyou
0 Comments
Answers (1)
Steven Lord
on 7 Jul 2020
lcc is not a Constant property of the clCore class, so each instance of clCode has its own lcc property.
So which clCode instance's lcc property do you want to use in the get.dpu method in the clPrimary class?
As an analogy, it doesn't really make sense to ask for human's name. It makes sense to ask for one particular human's name.
Now if one of those properties of the clPrimary class instance whose dpu property you're asking to compute contains a clCore class instance, asking for the lcc property of that instance makes sense.
In the human analogy, while it doesn't make sense for you to ask for human's mother's name it would make sense for you to ask for my mother's name. In this analogy, I am an instance of the human class.
2 Comments
kanuri venkata mohana
on 7 Jul 2020
Edited: kanuri venkata mohana
on 7 Jul 2020
Steven Lord
on 7 Jul 2020
The identifier objCore used in the get.wsw method is undefined in this code.
If you want to use the lcc property of one specific object objCore you can do that so long as that object is available to the method (stored as a property of the objWS object that isa clSec would be the easiest way to do that.)
If you want to use the lcc property of the objCore class then that property must be Constant and every single instance of the objCore class will have the same value of that property.
The human class has a property name, but each instance of human has a different value for that property.
The human class has a Constant property hasDNA. It doesn't matter whether you ask the human class, the steve_lord instance of human, or the kanuri_venkata_mohana instance of human. Asking each of those questions will say that the hasDNA property is true.
See Also
Categories
Find more on Construct and Work with Object Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!