how can i change varable in class automatically

1 view (last 30 days)
i defined two classes:
class A has an attribute x
class B has an attribute y
for example:
ma=A(3);
mb=B(ma);
so now as defined above, ma.x=3, mb.y.x=3.
the question is, if i want to change ma.x to 6. like use code :ma.x=3. how can i let the mb.y.x change automatically to 6 too?

Answers (1)

Prabhanjan Mentla
Prabhanjan Mentla on 26 Nov 2020
Hi,
Here's the sample code of classA and classB. The main point here is change in the variable of one class reflected in both the classes.
classdef classA < matlab.mixin.Copyable
properties
x=0;
end
methods
function a = classA(x)
a.x = x;
end
end
end
classdef classB < classA
properties
y=0;
end
methods
function b = classB(x,y)
b@classA(x);
b.y = y;
end
end
end
>> a = classA(7);
>> b = classB(a,8)
b =
classB with properties:
y: 8
x: [1×1 classA]
>> b.x
ans =
classA with properties:
x: 7
>> a.x=9
a =
classA with properties:
x: 9
>> b.x
ans =
classA with properties:
x: 9
You can try similar concepts to get work done and I would recommend you to check Object-Oriented Programming course get started with the Object-Oriented Concepts on MATLAB.
Hope this helps.

Categories

Find more on Programming in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!