how can i change varable in class automatically

1 次查看(过去 30 天)
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?

回答(1 个)

Prabhanjan Mentla
Prabhanjan Mentla 2020-11-26
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.

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by