MATLAB Answers

How to use properties of a class in its own class and also in other classes?

7 views (last 30 days)
kanuri venkata mohana
kanuri venkata mohana on 7 Jul 2020
Commented: Steven Lord on 7 Jul 2020
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

Sign in to comment.

Answers (1)

Steven Lord
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
kanuri venkata mohana on 7 Jul 2020
Hi Steven, thankyou for your reply. In get.dpu method i want to use one of the properties of clCore. the lcc property which i used in clPrimary is from clCore. The wpw which i used in clCore is from clPrimary. both lcc,wpw are independent parameters. Here i wnat to show u that the parameters in the two classes are depending on eachother.
Usually i can make it using clPrimary<clcore but the problem is if i want to calculate some other parameter in clCore using an independent parameter of clPrimary i could not do it. The actual problem is i have to use these kind of parameters in around 10 classes. Each time when i want to call the property of a particular class how to do that.
Here iam explaining you also with another using third class.
classdef clSec
properties
dsu
end
properties(Dependent)
wsw
dsw
end
methods
function objWS=clSec(dsu)
objWS.dsu=dsu;
end
function wsw=get.wsw(objWS)
wsw=objWS.dsu+objWP.wpw+objCore.lcc
end
end
end
I know my function at wsw is wrong but i want to show i want my value at wsw is a combination of parameters from different classes.
How to make an interface between my objects and parameters so that i can use any parameters in any class i want.
Thankyou.
Steven Lord
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.

Sign in to comment.

Tags