フィルターのクリア

Are nested classes possible?

41 ビュー (過去 30 日間)
Vincent
Vincent 2011 年 9 月 1 日
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

回答 (2 件)

Matt Landow*
Matt Landow* 2011 年 9 月 1 日
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 件のコメント
Vincent
Vincent 2011 年 9 月 1 日
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'

サインインしてコメントする。


Daniel Shub
Daniel Shub 2011 年 9 月 1 日
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>>

カテゴリ

Help Center および File ExchangeConstruct and Work with Object Arrays についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by