필터 지우기
필터 지우기

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 CenterFile Exchange에서 Construct 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