Filter löschen
Filter löschen

Are nested classes possible?

41 Ansichten (letzte 30 Tage)
Vincent
Vincent am 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

Antworten (2)

Matt Landow*
Matt Landow* am 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 Kommentar
Vincent
Vincent am 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'

Melden Sie sich an, um zu kommentieren.


Daniel Shub
Daniel Shub am 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>>

Kategorien

Mehr zu Construct and Work with Object Arrays finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by