how can i change varable in class automatically

3 visualizaciones (últimos 30 días)
Zeyang Lei
Zeyang Lei el 20 de Nov. de 2020
Respondida: Prabhanjan Mentla el 26 de Nov. de 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?

Respuestas (1)

Prabhanjan Mentla
Prabhanjan Mentla el 26 de Nov. de 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.

Categorías

Más información sobre Construct and Work with Object Arrays en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by