Error when using get method (MATLAB:cl​ass:Depend​entGet)

4 ビュー (過去 30 日間)
Jan-Andrea Bard
Jan-Andrea Bard 2020 年 5 月 20 日
Consider the following Class and the short script to build the blocks of a table. The idea behind the script is that T stands for the whole table where as U and V are two subblocks, constituting the table. Why does Matlab throw this error when I query V.top?
In class 'TestComponent', the get method for Dependent property 'top' attempts to access the stored property value. Dependent properties don't store a value and can't be accessed from their get method.
Script:
T = TestComponent
U = TestComponent
V = TestComponent
T.add(U)
T.add(V)
V.top
Class:
classdef TestComponent < handle
properties
Children
previousPeer
topRoot = 1
height = 1
end
properties (Dependent)
top
end
methods
function val = get.top(obj)
if ~isempty(obj.previousPeer)
val = obj.previousPeer.top + obj.previousPeer.height;
else
val = obj.topRoot;
end
end
function add(obj,child)
if isempty(obj.Children)
obj.Children{1} = child;
else
child.previousPeer = obj.Children{end};
obj.Children{end + 1,1} = child;
end
end
end
end

回答 (0 件)

Community Treasure Hunt

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

Start Hunting!

Translated by