主要内容

软件包创建名称空间

Package Folders

Packages are special folders that can contain class folders, function, and class definition files, and other packages. The names of classes and functions are scoped to the package folder. A package is a namespace within which names must be unique. Function and class names must be unique only within the package. Using a package provides a means to organize classes and functions. Packages also enable you to reuse the names of classes and functions in different packages.

Note

在MATLAB之前创建的类不支持包万博1manbetx裹®版本7.6(即不使用的类classdef)。

Package folders always begin with the+character. For example,

+mypack +mypack/pkfcn.m%a软件包函数 +mypack/@myclass%类文件夹

顶级软件包文件夹的母体必须在MATLAB路径上。

列出包装的内容

使用该软件包的内容使用帮助command:

帮助event
Contents of event: EventData - event.EVENTDATA Base class for event data PropertyEvent - event.PROPERTYEVENT Event data for object property events listener - event.LISTENER Listener object proplistener - event.PROPLISTENER Listener object for property events

You can also use thewhatcommand:

what event
Classes in directory Y:xxx\matlab\toolbox\matlab\lang\+event EventData PropertyEvent listener proplistener

Internal Packages

MathWorks®保留命名的包装的使用内部的for utility functions used by internal MATLAB code. Functions that belong to an内部的包装仅用于数学工程。使用属于一个的功能或类内部的包裹不建议。这些功能和类不能保证以一致的方式从一个发行版到下一个功能。这些功能和类都可以在随后的任何版本中从MATLAB软件中删除,恕不另行通知,并且在产品发行说明中没有文档。

Referencing Package Members Within Packages

All references to packages, functions, and classes in the package must use the package name prefix, unless you import the package. (SeeImport Classes。)例如,调用此软件包函数:

+mypack/pkfcn.m

使用此语法:

z = mypack.pkfcn(x,y);

Definitions do not use the package prefix. For example, the function definition line of thepkfcn.mfunction would include only the function name:

功能z = pkfcn(x,y)

Define a package class with only the class name:

ClassDef myclass

但请使用包装前缀来称呼它:

obj = mypack.myClass(arg1,arg2,...);

呼叫类方法不需要软件包名称,因为您具有类的对象。您可以使用点或功能符号:

obj.mymethod(arg)myMethod(obj,arg)

静态方法需要完整的班级名称,其中包括包装名称:

mypack.myclass.stmethod(arg)

参考包装成员来自包装外部

Functions, classes, and other packages contained in a package are scoped to that package. To reference any of the package members, prefix the package name to the member name, separated by a dot. For example, the following statement creates an instance ofMyClass, which is contained inmypackpackage.

obj = mypack.MyClass;

访问班级成员 - 各种情况

本节向您展示了如何从外部包装中访问各种软件包成员。假设你有一个包裹mypackwith the following contents:

+mypack +mypack/myFcn.m +mypack/@MyFirstClass +mypack/@MyFirstClass/myFcn.m +mypack/@MyFirstClass/otherFcn.m +mypack/@MyFirstClass/MyFirstClass.m +mypack/@MySecondClass +mypack/@MySecondClass/mysecondclass.m +mypack/ +mysubpack +mypack/ +mysubpack/myfcn.m

Invoke themyFcn功能在mypack:

mypack.myfcn(arg)

创建每个类的实例mypack:

其中obj1 = mypack.MyFirstClass;methoda = mypack.MySecondClass(arg);

Invoke themyFcn包装中的功能mysubpack:

mypack.mysubpack.myfcn(arg1,arg2);

如果mypack.myfirstclasshas a method calledmyFcn,像任何方法一样调用对象的任何方法:

obj = mypack.MyFirstClass; myFcn(obj,arg);

如果mypack.myfirstclasshas a property calledmyprop,使用点表示法和对象分配它:

obj = mypack.MyFirstClass; obj.MyProp = x;

包裹和MATLABPath

You cannot add package folders to the MATLAB path, but you must add the package parent folder to the MATLAB path. Package members are not accessible if the package parent folder is not on the MATLAB path, even if the package folder is the current folder. Making the package folder the current folder is not sufficient to add the package parent folder to the path.

包装成员仍然瞄准了包装。始终使用软件包名称参考软件包成员。或者,将软件包导入您调用包成员的函数,请参阅Import Classes.

Package folders do not shadow other package folders that are positioned later on the path, unlike classes, which do shadow other classes. If two or more packages have the same name, MATLAB treats them all as one package. If redundantly named packages in different path folders define the same function name, then MATLAB finds only one of these functions.

解决冗余名称

Suppose a package and a class have the same name. For example:

fldr_1/+foo fldr_2/@foo/foo.m

A call towhich fooreturns the path to the executable class constructor:

>>哪个flood fldr_2/@foo/foo.m

A function and a package can have the same name. However, a package name by itself is not an identifier. Therefore, if a redundant name occurs alone, it identifies the function. Executing a package name alone returns an error.

Package Functions vs. Static Methods

In cases where a package and a class have the same name, a package function takes precedence over a static method. For example, path folderfldra包含一个软件包功能和路径文件夹fldrb包含类静态方法:

fldra/+foo/bar.m % bar is a function in package foo fldrB/@foo/bar.m % bar is a static method of class foo

A call to哪个foo.barreturns the path to the package function:

whichfoo.bar
fldra \+foo \ bar.m%包装功能

如果相同的路径文件夹同时包含具有相同名称的包装和类文件夹,则包装函数优先于静态方法。

fldr/@foo/bar.m% bar is a static method of class foofldr/+foo/bar.m% bar is a function in package foo

A call to哪个foo.barreturns the path to the package function:

whichfoo.bar
fldr/+foo/bar.m

如果路径文件夹fldr包含一个classdef文件foo定义静态方法barand the same folder contains a package+foo包含包装功能bar.

fldr/foo.m% bar is a static method of class foofldr/+foo/bar.m% bar is a function in package foo

A call to哪个foo.barreturns the path to the package function:

whichfoo.bar
fldr/+foo/bar.m

相关话题