主要内容

Folders Containing Class Definitions

路径上的类定义

要调用类方法,类定义必须在MATLAB上®path, as described in the next sections.

类和路径文件夹

There are two types of folders that can contain class definition files.

  • 路径文件夹 - 文件夹在MATLAB路径上,文件夹名称不以一个@特点。当您想要一个文件夹中的多个类和功能时,请使用此类型的文件夹。整个类定义必须包含在一个文件中。

  • 类文件夹 - 文件夹名称以 @字符开始,然后是类名称。该文件夹不在MATLAB路径上,而是其父文件夹在路径上。当您想将多个文件用于一个类定义时,请使用此类型的文件夹。

看到path有关MATLAB路径的信息的功能。

使用路径文件夹

包含类定义文件的文件夹在MATLAB路径上。因此,放置在路径文件夹中的类定义对任何普通功能在优先级方面的行为 - MATLAB路径上的第一次出现名称优先于同一名称的所有后续出现。

每个类定义文件的名称必须与指定的类的名称匹配classdef关键词。使用路径文件夹消除了为每个类创建单独的类文件夹的需求。但是,整个类定义(包括所有方法)必须包含在一个文件中。

假设您在一个文件夹中定义了三个类:

。。。/path_folder/MyClass1.m .../path_folder/MyClass2.m .../path_folder/MyClass3.m

To use these classes, addpath_folder到您的Matlab路径:

addpathpath_folder

Using Class Folders

类文件夹的名称始终以@字符后跟文件夹名称的类名。类文件夹必须包含在路径文件夹中,但是类文件夹不在MATLAB路径上。将类定义文件放入类文件夹中,该文件还可以包含单独的方法文件。类定义文件必须与类文件夹具有相同的名称(没有@特点)。

。。。/parent_folder/@MyClass/MyClass.m .../parent_folder/@MyClass/myMethod1.m .../parent_folder/@MyClass/myMethod2.m

每个文件夹仅定义一个类。所有文件都有一个.m或者。p扩大。For MATLAB versions R2018a and later, standalone methods can be live functions with a.mlx扩大。

当您想将多个文件用于类定义时,请使用类文件夹。MATLAB将类文件夹中的任何功能文件视为类的方法。功能文件可以是MATLAB代码(.m),实时代码文件格式(.mlx),MEX函数(平台相关扩展)和P代码文件(。p)。

MATLAB明确将类文件夹中的任何文件标识为该类的方法。这使您可以使用更模块化的方法来创作课程的方法。

每个文件的基本名称必须是有效的MATLAB函数名称。有效功能名称以字母字符开头,可以包含字母,数字或下划线。有关更多信息,请参阅Methods in Separate Files

功能s in Private Folders Within Class Folders

私有文件夹包含仅从在正上方的文件夹中定义的功能中访问的功能privatefolder. Any functions defined in aprivate类文件夹中的文件夹只能从类的方法调用。这些功能可以访问班级的私人成员,但本身不是方法。他们不需要将对象作为输入传递,只能使用函数表示法调用。使用功能private文件夹可以当你需要助手函数called from multiple methods of your class.

如果a class folder contains aprivate文件夹,只有该文件夹中定义的类才能访问在该文件中定义的功能privatefolder. Subclasses do not have access to superclass private functions. For more information on private folders, see私人功能

如果you want a subclass to have access to the private functions of the superclass, define the functions as protected methods of the superclass. Specify the methods with theAccess属性设置为protected

派遣私人文件夹中的方法

如果a class defines functions in aprivatefolder that is in a class folder, then MATLAB follows these precedence rules when dispatching to the private functions versus the methods of theclassdef文件:

  • Using dot notation (obj.methodName),一个功能private文件夹优先于在classdef文件。

  • 使用函数符号(methodName(obj)), a method defined in theclassdeffile takes precedence over the function in theprivatefolder.

No Class Definitions in Private Folders

You cannot put class definitions (classdef文件)在私有文件夹中,因为这样做将无法满足类或路径文件夹的要求。

Class Precedence andMATLABPath

When there are multiple class definitions with the same name, the file location on the MATLAB path determines precedence. The class definition in the folder that comes first on the MATLAB path always takes precedence over any classes that are later on the path, whether or not the definitions are contained in a class folder.

A function with the same name as a class in a path folder takes precedence over the class if the function is in a folder that is earlier on the path. However, a class defined in a class folder (@-folder) takes precedence over a function of the same name, even if the function is defined in a folder that is earlier on the path.

例如,考虑使用以下文件夹和文件的路径。

在路径中订购 Folder and File File Defines

1

FLDR1/Foo.m

ClassFoo

2

fldr2/Foo.m

功能Foo

3

fldr3/@foo/Foo.m

ClassFoo

4

fldr4/@foo/bar.m

Methodbar

5

fldr5/foo.m

ClassFoo

MATLAB应用此逻辑来确定哪个版本的Foo致电:

ClassFLDR1/Foo.m优先于课程fldr3/@foo因为:

  • FLDR1is beforefldr3on the path, andFLDR1/Foo.mis a class.

Classfldr3/@foo取得优先functionfldr2/Foo.m因为:

  • fldr3/@foois a class in a class folder.

  • fldr2/Foo.m不是课。

  • Classes in class folders take precedence over functions.

功能fldr2/Foo.m取得优先classfldr5/foo.m因为:

  • fldr2来了classFLDR5on the path.

  • fldr5/foo.m不在类文件夹中。

  • 在类文件夹中未定义的类遵守函数的路径顺序。

Classfldr3/@foo取得优先FLDR4/@Foo因为:

  • fldr3来了FLDR4on the path.

如果fldr3/@foo/Foo.m包含在版本7.6之前创建的MATLAB类(也就是说,类不使用classdefkeyword), thenfldr4/@foo/bar.m成为Fooclass defined infldr3/@foo

Previous Behavior of Classes Defined in Class Folders

In MATLAB Versions 5 through 7, class folders do not shadow other class folders having the same name, but residing in later path folders. Instead, the class uses the combination of methods from all class folders having the same name to define the class. This behavior is no longer supported.

For backward compatibility, classes defined in class folders always take precedence over functions and scripts having the same name. This precedence applies to functions and scripts that come before these classes on the path.

Changing Path to Update Class Definition

MATLAB只能识别一个类的定义为当前定义。更改MATLAB路径可以更改类的定义文件(请参阅path)。如果不存在旧定义的实例(即不再是路径上的定义),则MATLAB立即将新文件夹识别为当前定义。但是,如果您在更改路径之前具有类的现有实例,则MATLAB是否使用新文件夹中的定义取决于如何定义新类。如果新定义是在类文件夹中定义的,则MATLAB立即将新文件夹识别为当前类定义。但是,对于在路径文件夹中定义的类@文件夹),您必须在MATLAB识别新文件夹为当前类定义之前清除类。

类文件夹中的类定义

假设您定义了两个名称的类的版本Foo在two folders,FLDAFLDB

flda/@foo/foo.m fldb/@foo/foo.m

Add folderFLDA到达路径的顶部。

addpathFLDA

创建类的实例Foo。MATLAB使用FLDA/@Foo/Foo.mas the class definition.

a = foo;

Change the current folder toFLDB

cdFLDB

当前文件夹始终是路径上的。因此,MATLAB发现FLDB/@Foo/Foo.m作为班级的定义Foo

b = foo;

MATLAB automatically updates the existing instance,a,在FLDB

Class Definitions in Path Folders

假设您定义了两个名称的类的版本Foo在two folders,FLDAFLDB,但请勿使用类文件夹。

flda/foo.mfldb/foo.m

Add folderFLDA到达路径的顶部。

addpathFLDA

创建类的实例Foo。MATLAB使用flda/foo.mas the class definition.

a = foo;

Change the current folder toFLDB

cdFLDB

The current folder is effectively the top of the path. However, MATLAB does not identifyfldb/foo.m作为班级的定义Foo。MATLAB继续使用原始类定义,直到您清除类。

To use the definition ofFoo折叠, clearFoo

清除Foo

MATLAB automatically updates the existing objects to conform to the class definition inFLDB。通常,清除实例变量是不必要的。

相关话题