How to share attribue with several objects ?
5 views (last 30 days)
Show older comments
Hello,
I want to share attribues with several objects (Object-oriented programming). I would know how write the line wuth "????" in my example.
classdef Main < handle
properties
calcul1
calcul2
end
methods
function obj = Main()
obj.calcul1 = Calcul1();
obj.calcul2 = Calcul2();
end
end
end
classdef Calcul1< handle
properties
data_calcul1
end
methods
function obj = Calcul1()
end
function fct_lambda(obj)
obj.calcul1 = equation(data_calcul1);
end
end
end
end
classdef Calcul2< handle
properties
data_calcul2
end
methods
function obj = Calcul2()
end
function fct_lambda(obj)
obj.calcul2 = equation(data_calcul1); ????????
end
end
end
end
2 Comments
Adam
on 22 Nov 2018
Edited: Adam
on 22 Nov 2018
Can you explain more what you are trying to do? You have 3 different classes there and you need an object of a class to call functions on it or use properties.
Your function on the Calcul2 class is attempting to assign to what appears to be a property of the 'Main' class. You need an object of this class created somewhere if this is what you want. You are also using data_calcul1 to pass to your equation function (which I assume is defined externally), but again this variable does not exist within this scope.
For interaction between objects you actually have to have the objects, not just classes. An object is an instance of a class and you need these instances if you want to access and assign properties to/from objects of different classes.
at the moment your design is too confused for me to understand exactly what it is you are wanting to do and what the role of the 3 classes are. You create objects of the 2nd two classes in the constructor of the first, but they then don't do anything. Their constructors are both empty and the Main class does nothing with the objects so you just have two empty objects within Main.
Guillaume
on 22 Nov 2018
Ghislain's comment mistakenly posted as an answer moved here:
Thank you for your first reply. Sorry if my question isn't clear.I'm just starting to learn the OOP. I rewrite my problem and I comment my problem. I hope that it will clear.
classdef Main < handle
properties
calcul1
calcul2
end
methods
function obj = Main()
obj.calcul1 = Calcul1();
obj.calcul2 = Calcul2();
end
end
end
classdef Calcul1 < handle
properties
data
end
methods
function obj = Calcul1()
fct_lambda(obj);
end
function fct_lambda(obj)
x = 2;
y = 3;
obj.data.x = x^2;
obj.data.y = y^2;
end
end
end
classdef Calcul2 < handle
properties
data
end
methods
function obj = Calcul2()
fct_lambda(obj);
end
function fct_lambda(obj)
obj.data.x = % main.calcul1.data.x^2 I would like get the value of calcul1
obj.data.y = % main.calcul2.data.y^2
end
end
end
main = Main();
Answers (2)
Guillaume
on 22 Nov 2018
I don't think you've grasped OOP yet. As Adam asked you need to explain what you're trying to do, with words, not code.
As it is, if you want to access public properties of an instance of the class calcul1, you'll have to pass that instance to the constructor of calcul2, so:
classdef Main < handle %is handle needed
properties %public by default
calcul1
calcul2
end
methods
function obj = Main() %constructor
obj.calcul1 = Calcul1; %create a Calcul1 instance and store in Main
obj.calcul2 = Calcul2(obj.calcul1); %pass instance to Calcul2 constructor
end
end
end
%class calcul1 details don't matter
classdef Calcul2 < handle
properties
data
end
methods
function obj = Calcul2(calcul1obj)
obj.fct_lambda(calcul1obj);
end
function fct_lambda(obj, calcul1obj)
obj.data.x = calcul1obj.data.x^2;
obj.data.y = calcul1obj.data.y^2;
end
end
end
Instead of passing the calcul1 instance to the constructor of calcul2, you could pass the Main instance that you're constructing. I wouldn't recommend it though, it's not a good idea to pass around an object that is still under construction.
0 Comments
See Also
Categories
Find more on Code Execution 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!