Prev: can imfilter be call only one time for the following calculation?
Next: how to declare a method in a class that can change its properties
From: Quoc Nguyen on 19 Jul 2010 15:46 Hi everyone, I am new to object-oriented programming. Please help. My class is defined as follows: ------------------------- (myclass.m) classdef myclass properties a = []; end methods function obj = myclass(n) obj.a = n; end function y = inc(obj) obj.a = obj.a + 1; y = obj.a; end end end ------------- (workspace) >> x = myclass(1); >> y = inc(x) y = 2 >> x.a ans = 1 --------- In the m-code above, I expect that x.a is equal to 2 after executing y = inc(x). How can I modify the code so that whenever the method inc() is executed, the property a is also updated according to the command "obj.a = obj.a + 1;" ? Thanks |