Azzera filtri
Azzera filtri

Are nested classes possible?

41 visualizzazioni (ultimi 30 giorni)
Vincent
Vincent il 1 Set 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

Risposte (2)

Matt Landow*
Matt Landow* il 1 Set 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 Commento
Vincent
Vincent il 1 Set 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'

Accedi per commentare.


Daniel Shub
Daniel Shub il 1 Set 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>>

Categorie

Scopri di più su Construct and Work with Object Arrays in Help Center e File Exchange

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by