How to use properties of a class in its own class and also in other classes?
9 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(1 个)
Steven Lord
2020-7-7
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 个评论
Steven Lord
2020-7-7
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.
另请参阅
类别
在 Help Center 和 File 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!