On Sat, Aug 15, 2009 at 3:04 AM, Jaroslav Hajek
<address@hidden> wrote:
Also, does Matlab really auto-forward the field references to parent
classes? I can't find that anywhere in the Matlab docs. For instance,
if a class bar inherits from foo, which has a field xx, does this.xx
really work inside bar methods, or must one do this.foo.xx?
Apparently, you cannot access fields of a parent class using either this.foo.xx nor this.xx from a subclass. A subclass can have it's own this.xx but there doesn't appear to be any forwarding of xx at all. To me it looks like inheritance only has to do with function lookup.
In the following example, look for @foo/access.m reading the value of bar.xx instead of bar.foo.xx and not substituting bar.foo.yy for the missing bar.yy. Also look for @bar/access1.m also not forwarding bar.yy into bar.foo.yy. In @bar/access2.m, any direct access to bar.foo.xx or bar.foo.yy is not allowed.
@foo/foo.m:
function obj = foo()
obj.xx = 10 ;
obj.yy = 20 ;
obj = class(obj, 'foo') ;
end
@foo/access.m:
function access(obj)
disp(obj.xx) ;
disp(obj,yy) ;
end
@bar/bar.m:
function obj = bar()
obj.xx = 30 ;
obj = class(obj, 'bar', foo() ) ;
end
@bar/access1.m:
function access1(obj)
disp(obj.xx) ;
disp(obj.yy) ;
end
@bar/access2.m:
function access2(obj)
disp(obj.foo.xx)
disp(obj.foo.yy)
end
>> b = bar()
b =
bar object: 1-by-1
>> access(b)
10
??? Undefined function or variable 'yy'.
Error in ==> foo.access at 3
disp(obj,yy) ;
>> access1(b)
30
??? Reference to non-existent field 'yy'.
Error in ==> bar.access1 at 3
disp(obj.yy) ;
>> access2(b)
??? Access to an object's fields is only permitted within its methods.
Error in ==> bar.access2 at 2
disp(obj.foo.xx)