Clear Filters
Clear Filters

Are nested classes possible?

42 views (last 30 days)
Vincent
Vincent on 1 Sep 2011
Is it possible to have a nested class, that's only available to the class that it created?
For example I would say in my workspace(/example/):
>> phone = device
??? Undefined function or variable 'setting'.
But then i get this error. The used paths and code:
/example/@device/device.m
/example/@device/@setting/setting.m
device.m:
classdef device
properties
name
owner
settings = setting
end
end
setting.m:
classdef setting
properties
firmware
password
ringtone
end
end

Answers (2)

Matt Landow*
Matt Landow* on 1 Sep 2011
easy fix
just have setting instead of settings.
classdef device
properties
name
owner
setting
end
end
then you can do calls like, device.setting.firmware=[1 2 3 4 5];
  1 Comment
Vincent
Vincent on 1 Sep 2011
Thanks, it works then indeed that I can do calls like that. But then the setting is not of a class setting, but a normal struct. Then it is still possible to make typos. So I'm looking how to make class again.
>> phone=device
phone =
device
Properties:
name: []
owner: []
setting: []
Methods
>> class(phone.setting)
ans =
double
>> phone.setting.firmware = '1.1a'
phone =
device
Properties:
name: []
owner: []
setting: [1x1 struct]
Methods
>> phone.setting.dummy = 'should not be possible'
phone =
device
Properties:
name: []
owner: []
setting: [1x1 struct]
Methods
>> phone.setting
ans =
firmware: '1.1a'
dummy: 'should not be possible'

Sign in to comment.


Daniel Shub
Daniel Shub on 1 Sep 2011
You cannot define a class in a private folder:
<http://www.mathworks.com/help/releases/R2011a/techdoc/matlab_oop/brfynrp-1.html >
and therefore it seems unlikely you can make a truly private class. If you are just trying to avoid namespace conflicts you can use a package href=""<http://www.mathworks.com/help/releases/R2011a/techdoc/matlab_oop/brfynt_-1.html</a>>

Categories

Find more on Construct and Work with Object Arrays 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!