how can i change varable in class automatically
조회 수: 3 (최근 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?
댓글 수: 0
답변 (1개)
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.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Construct and Work with Object Arrays에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!