How to get Dependent Property depending on properties of objects of different class

4 vues (au cours des 30 derniers jours)
See the code below:
Ex_ObjA.m-->
classdef Ex_ObjA
properties
a
end
methods
function Obj=Ex_ObjA(t)
Obj.a = t;
end
end
end
Ex_ObjBC.m-->
classdef Ex_ObjBC
properties
b
end
properties (Dependent = true, SetAccess = public)
c
end
methods
function Obj=Ex_ObjBC(t)
Obj.b = t;
end
function c=get.c(Obj,s1) % error: Get methods must have exactly one input
c = Obj.b + s1.a;
end
end
end
I tried to do following:
s1 = Ex_ObjA(2);
s2 = Ex_ObjBC(3);
s2.c
Not successful, because "Get methods must have exactly one input". So I can pass the s1.a to Ex_ObjBC to get s1.c?
Much appreciation!!!

Réponse acceptée

Matt J
Matt J le 2 Oct 2013
Modifié(e) : Matt J le 2 Oct 2013
There's no apparent reason why c should be a property, Dependent or otherwise. You could just write the class this way,
classdef Ex_ObjBC
properties
b
end
methods
function Obj=Ex_ObjBC(t)
Obj.b = t;
end
function out=c(Obj,s1)
out = Obj.b + s1.a;
end
end
end
  4 commentaires
J. Hu
J. Hu le 2 Oct 2013
Thanks for your explanation. Since c is a value depending on properties of different classes, what you would suggest placing the function to get c - in one of the classes of the properties it depends on or none of them? For example, if c depends on 10 properties, 9 of those belong to one class A and one comes from another class B. So c is HIGHLY depending on the class A - and that is the reason I wanted to put it is a dependent property of class A.
Upon your suggestion, I did define the function like c=getc(Obj,s1) and call it using s2.getc(s1). It works but I do not understand why the inputs have to be like that, one is (Obj,s1) in the function definition and the caller is getc(s1) - not the same. I know it works but why? Seems Obj is internal input of s2. Thanks...
Matt J
Matt J le 2 Oct 2013
s2.getc(s1) is equivalent to getc(s2,s1)

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Software Development Tools dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by