[basic class syntax question] how can i fix this code
Show older comments
hello,
now i am studying matlab class code
but i have some problem in my code ,also i can not find which points make errors.
there two classes(example1,example2) that have some methods(example1 is construct instance of example2 and call methods from example1)
classdef example1
properties
value
value1
value2
results
end
methods
function obj = example1(value1,value2)
obj.value1 = example2(value1)%construct other class
obj.value2 = example2(value2)%construct other class
end
function add_2(obj, somevalue)% to activate other class method
example2.add_3(obj.value1,somevalue) % run the other class methods
end
end
end
and other class
classdef example2
properties
value
limit
end
methods
function obj = example2(limitvalue) % definition of the secound class
obj.limit = limitvalue
obj.value = 0
end
end
methods (Static) % when i deleted the '(static)' it makes a error, i don't understand why this error apper??
function add_3(obj, addvalue)
obj.value = obj.value + addvalue
if obj.value > obj.limit
%disp(['add max',str(obj)])
disp('over')
end
end
end
end
first i assign the first class
test1 = example1(10,10) %first i assign the first class
test1.add_2(3) % assign the secound class value as 3
test1.add_2(3) % assign the updated value to secound class value
% i want to assign 3+3 value in secound property test1.value1.value but still "3"
% how can i fix??
4 Comments
Walter Roberson
on 4 Feb 2021
example2.add_3(obj.value1,somevalue) % run the other class methods
should probably not have the example2 prefix. value1 should be a member of example2 class so add_3 should automatically be pulled out due to the type
InYoung Chung
on 4 Feb 2021
Walter Roberson
on 4 Feb 2021
example2.add_3(obj.value1,somevalue) % run the other class methods
change to
add_3(obj.value1,somevalue) % run the other class methods
InYoung Chung
on 5 Feb 2021
Accepted Answer
More Answers (0)
Categories
Find more on Graphics Performance in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!