how can i change varable in class automatically

1 Ansicht (letzte 30 Tage)
Zeyang Lei
Zeyang Lei am 20 Nov. 2020
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?

Antworten (1)

Prabhanjan Mentla
Prabhanjan Mentla am 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.

Kategorien

Mehr zu Programming finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by