calling dependent properties from one class to another

2 ビュー (過去 30 日間)
kanuri venkata mohana
kanuri venkata mohana 2020 年 9 月 10 日
回答済み: Steven Lord 2020 年 9 月 11 日
Hi guys,
I want to call dependent properties from one class to another in matlab OOP. Could anyone suggest me how to do that.
I just want to call all my dependent properties. Since i calculated them using get function iam unable to understand how to call them. Could anyone give me some idea.
Thankyou
  1 件のコメント
Steven Lord
Steven Lord 2020 年 9 月 10 日
Are you trying to access the dependent properties through the class name or through a class instance?
The people class defines properties height and weight and a dependent property bodyMassIndex calculated from height and weight.
It doesn't make sense to ask for the bodyMassIndex for the people class.
It makes sense to ask for the bodyMassIndex of a specific instance of the people class.
If that's not the difficulty you're experiencing trying to access dependent properties, please show a small example that simulates the real problem you're trying to solve/model and explain the difficulty using that small example.

サインインしてコメントする。

回答 (2 件)

Matt J
Matt J 2020 年 9 月 10 日
編集済み: Matt J 2020 年 9 月 10 日
You access it the same as you would a non-Dependent property,
object.property
  5 件のコメント
Matt J
Matt J 2020 年 9 月 11 日
So, it's a different problem now? Earlier you said you were getting an error message about a non-existent property reference.
kanuri venkata mohana
kanuri venkata mohana 2020 年 9 月 11 日
i described about the different cases of my error. i have also explained about the non-existent field property reference and also the way i tried.

サインインしてコメントする。


Steven Lord
Steven Lord 2020 年 9 月 11 日
classdef fitFunction
methods(Static)
function f=fit(objDS)
P=[0.456,0.2030]
objCore.Matcore=objDS.Matcore;
objCore.Wm=objDS.Wm;
% my core parameters have to be updated
objCore.depth=P(1);
objCore.length=P(2);
% i need to call my dependent properties
c1=lte(objCore.depth,objDS.dxma)
end
end
end
You created an object objCore in the base workspace. This function does not operate on that object. The assignments to objCore make a struct array in this function's workspace. So if you were to ask for objCore.area in this function, that struct doesn't have a field by that name.
There's a scientist named Steven Lord at the SETI Institute. Even though we share a name, I am not him and he is not me. Similarly just because the object in the base workspace and this identifier in the function share a name, they are not the same thing.
If you want this function to operate on an objCore object, it needs to accept that object as an input or to create one. [Technically there is a way for it to "reach into" its caller's workspace or the base workspace, but I strongly discourage doing that sort of "action at a distance".]

カテゴリ

Help Center および File ExchangeFoundation and Custom Domains についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by