当前位置:网站首页>MATLAB 文件夹前面的+和@是干啥的 命名空间与函数的重载
MATLAB 文件夹前面的+和@是干啥的 命名空间与函数的重载
2022-07-27 21:49:00 【slandarer】
前言
当你打开MATLAB的toolbox文件夹时(工具箱存储路径),会发现里面文件夹名称前会有一些奇奇怪怪的符号:


???这里面的+和@是干啥的???
为了探究这个问题,我们建立了一个具有如下目录的文件夹:

当前文件夹──@cell──dispTest.m
|
├──@double──dispTest.m
|
├──+test1──+test2──test.m
| |
| └──test.m
|
└──untitled.m
命名空间:路径函数(目录结构体)
如果想要代码整洁条理,很多情况会把一些同一类的工具函数放在一个单独的文件夹里,即你要调用的函数在当前文件夹内的一个文件夹中,想在当前文件夹直接调用这个函数是做不到的,要咋办?
很多人会选择直接addpath,但这样的话那些工具函数就不止该项目可以调用,其他位置的其他项目也能调用,这样有可能导致命名冲突。
那么一个很简单的方法就是将装工具函数的文件夹命名为+fordername的形式,就是前面有个+的形式,实际上就是文件夹路径作为命名空间。
对于我们前面提出的目录的以下部分:
当前文件夹──+test1──+test2──test.m
| |
| └──test.m
|
└──untitled.m
我想要在untitled.m中调用+test1文件夹中的test.m,就可以直接通过以下代码调用:
test1.test()
当然像目录所示,+test1文件夹里还有个带加号的+test2文件夹,我们想要调用那个文件夹里的test.m,就可以通过如下方式:
test1.test2.test()
这种调用格式非常像是在调用结构体里的函数,这也是我将其称为目录结构体的原因。
函数的重载
当然接下来就要讲目录的这部分是啥:
当前文件夹──@cell──dispTest.m
|
├──@double──dispTest.m
|
└──untitled.m
很像是C++中的函数的重载哈,两个@classname的文件夹中右两个名字完全相同的函数。
注意这里说的是@加上类名称的格式,可以是MATLAB自带的类,也可以是自己定义的类函数,我们这里就设置了元胞类和双精度类命名的文件夹。
这么设置有啥用呢?这样设置后,比如我再调用dispTest.m函数的时候,如果输入变量是元胞数组,那么就会调用@cell文件夹里的该函数,如果输入变量是双精度类型,那么就会调用@double文件夹里的函数。
也就是说以下的两种写法,用的是不同函数:
dispTest(1)
dispTest({1})
结语
大概就是这个样子,对于+可以想做结构体,对于@可以想做重载,本人不同文件夹里的函数定义是这样的:
+test1\test.m
function test
disp('欢迎关注slandarer forder1')
end
+test1+test2\test.m
function test
disp('欢迎关注slandarer forder2')
end
@cell\dispTest.m
function dispTest(coe)
disp('is cell')
end
@double\dispTest.m
function dispTest(coe)
disp('is double')
end
因此在untitled.m中运行如下代码,效果如下:
test1.test()
test1.test2.test()
dispTest(1)
dispTest({
1})
欢迎关注slandarer forder1
欢迎关注slandarer forder2
is double
is cell
边栏推荐
- How to use FTP to realize automatic update of WinForm
- Is there a general formula for tens of millions of players? B station is underestimated as a hot money opportunity!
- 抖音直播监控-循环值守24小时-直播弹幕
- [unity] mapping 2D coordinates to ID
- 永州分析实验室建设选址概述
- What a beautiful rainbow
- JS promotion: array flattening in JS
- [book club issue 13] packaging format of audio and video files
- BUUCTF-RSA4
- Common errors reported by ant sword
猜你喜欢

新媒体内容输出方式-短视频

「图神经网络:基础、前沿与应用」最新IJCAI2022教程
![[极客大挑战 2019]RCE ME](/img/ff/aff58f40f2051f7415d1e16517f824.png)
[极客大挑战 2019]RCE ME

JS ATM机输出

【开发教程11】疯壳·ARM功能手机-定时器实验教程

荣耀多款产品齐发,笔记本MagicBook V 14售价6199元起

How to use C WinForm to copy files and display progress

Senior how to determine the standard of software test completion

传奇外网架设教程带图文解说——Gom引擎

What has the metauniverse of more than 30 years brought to us?
随机推荐
Have you ever seen this kind of dynamic programming -- the stock problem of state machine dynamic programming (Part 2)
What has the metauniverse of more than 30 years brought to us?
Unity implements simple Sketchpad drawing function (notes)
【zer0pts CTF 2022】 Anti-Fermat
XSS Payload 学习浏览器解码
2022最新抖音直播监控全套监控(五)商品详情监控
永州水质检测实验室建设:家具说明
数据中台的那些“经验与陷阱”
泵站远程监控
永州出入境检验实验室建设那些事
New media content output method - short video
北欧岗位制博士申请有多难?
require、loadfile、dofile、load、loadstring
MFC prompts that this application has requested the runtime to terminate it in an unused way editbox box has been deleted and is still in use
How to brush equipment in legendary clothes
机械工程物联网系统远程解决方案
The latest ijcai2022 tutorial of "figure neural network: foundation, frontier and application"
leetcode 452. Minimum Number of Arrows to Burst Balloons 用最少数量的箭引爆气球(中等)
[actf freshmen 2020] crypto AES
AI briefing how to use loss surfaces a model integration