Main Content

方法in Class Design

Class Methods

方法are functions that implement the operations performed on objects of a class. Methods, along with other class members support the concept of encapsulation—class instances contain data in properties and class methods operate on that data. This design allows the internal workings of classes to be hidden from code outside of the class, and thereby enabling the class implementation to change without affecting code that is external to the class.

方法have access to private members of their class including other methods and properties. This encapsulation enables you to hide data and create special interfaces that must be used to access the data stored in objects.

Examples and Syntax

For an example to get started writing classes, seeCreate a Simple Class

对于示例代码和语法,请参阅Define Class Methods and Functions

有关如何创建修改标准MATLAB的类的讨论®行为,看看方法That Modify Default Behavior.

For information on the use of @ and path directors and packages to organize your class files, seeClass Files and Folders

对于在多个文件中定义类时使用的语法,请参阅方法in Separate Files

各种方法

There are specialized kinds of methods that perform certain functions or behave in particular ways:

  • Ordinary methodsare functions that act on one or more objects and return some new object or some computed value. These methods are like ordinary MATLAB functions that cannot modify input arguments. Ordinary methods enable classes to implement arithmetic operators and computational functions. These methods require an object of the class on which to operate. See普通的方法.

  • Constructor methods是创建类对象的专门方法。构造函数方法必须具有与类相同的名称,并且通常使用从输入参数获得的数据初始化属性值。Class构造方法必须声明至少一个输出参数,该参数是正在构造的对象。第一个输出始终是正在构造的对象。看Class Constructor Methods

  • Destructor methods当对象被销毁时会自动调用,例如如果您调用delete(object)or there are no longer any references to the object. See处理类析构函数

  • Property access methods启用类以定义代码以在查询或设置属性值时执行。看Property Access Methods

  • 静态方法are functions that are associated with a class, but do not necessarily operate on class objects. These methods do not require an instance of the class to be referenced during invocation of the method, but typically perform operations in a way specific to the class. SeeStatic Methods

  • 转换方法are overloaded constructor methods from other classes that enable your class to convert its own objects to the class of the overloaded constructor. For example, if your class implements adouble方法,然后调用此方法而不是Double Class构造函数将您的类对象转换为Matlab Double对象。看Object Convertersfor more information.

  • Abstract methods定义无法实例化的类,但是用作定义众多子类使用的公共接口的方法。包含抽象方法的类通常被称为接口。看Abstract Classes and Class Membersfor more information and examples.

Method Naming

The name of a function that implements a method can contain dots (for example,set.PropertyName) only if the method is one of the following:

You cannot define property access or conversion methods as local functions, nested functions, or separately in their own files. Class constructors and package-scoped functions must use the unqualified name in the function definition; do not include the package name in the function definition statement.

相关话题