Main Content

Property Access Methods

属性提供对类数据的访问

In MATLAB®,,,,properties can have public access. Therefore, properties can provide access to data that the class design exposes to users.

Use property access methods to provide error checking or to implement side effects resulting from property access. Examples of access methods include functions that update other property values when setting the property or translate the format of a property value before returning the value.

有关访问方法语法的具体信息,请参见属性获取方法andProperty Set Methods

您可以使用属性验证来限制属性值的大小,类和其他方面。有关财产验证的信息,请参阅验证属性值

Performance Considerations with Access Methods

属性访问方法做添加f的开销unction call whenever accessing property values. If performance-critical access to properties occurs inside methods of the class, define private properties to store values. Use these values inside methods without any error checking. For less frequent access from outside the class, define publicDependent使用访问方法进行错误检查的属性。

For information on access methods used withDependentproperties, seeSet and Get Methods for Dependent Properties

属性集并获取方法

Property access methods execute specific code whenever the property value is queried or assigned a value. These methods enable you to perform various operations:

  • 执行代码之前assigning property values to perform actions such as:

    • Impose value range restrictions (验证属性值

    • Check for proper types and dimensions

    • Provide error handling

  • 执行代码之前returning the current values of properties to perform actions such as:

要控制哪些代码可以访问属性,请参见Property Attributes

WhenMATLAB调用访问方法

Property access methods execute automatically whenever you set or query the corresponding property values from outside the access method. MATLAB does not call access methods recursively. That is, MATLAB does not call the set method when setting a property from within that property’s set method, no matter what instance of the class is being modified. Similarly, MATLAB does not call the get method when querying the property value from within that property’s own get method.

笔记

您不能直接调用属性访问方法。当您访问属性值时,MATLAB调用这些方法。

Obtain the function handle for the set and get access methods from the propertymeta.property目的。这meta.propertysetMethodandGetMethodproperties contain the function handles that refer to these methods.

Restrictions on Access Methods

Define property access methods only:

  • For concrete properties (that is, properties that are not abstract)

  • 在定义属性的类中(除非该类中的属性是抽象的,在这种情况下,混凝土子类必须定义访问方法)。

MATLAB没有默认设置或获取属性访问方法。因此,如果您不定义属性访问方法,则MATLAB软件在分配或返回属性值之前不会调用任何方法。

定义后,只有集合和获取方法才能设置和查询实际属性值。看当调用设置方法时有关MATLAB不调用属性集方法的案例的信息。

笔记

属性集和获取访问方法不等于用户可靠andgetmethods used to set and query property values from an instance of the class. See实现属性的设置/获取界面有关用户可爱的信息andgetmethods.

Access Methods Cannot Call Functions to Access Properties

您只能从属性集中设置和获取属性值或获取访问方法。您不能从集合或获取方法调用另一个函数,并尝试从该功能访问属性值。

For example, an anonymous function that calls another function to do the actual work cannot access the property value. Similarly, an access function cannot call another function to access the property value.

Defining Access Methods

访问方法具有包括属性名称的特殊名称。所以,get.属性名称随时执行属性名称is referenced and放。属性名称随时执行属性名称被分配一个值。

Define property access methods in a methods block that specifies no attributes. You cannot call these methods directly. MATLAB calls these methods when any code accesses the properties.

Property access methods do not appear in the list of class methods returned by themethodscommand and are not included in themeta.class目的Methods财产。

访问方法功能处理

这propertymeta.property目的contains function handles to the property set and get methods.setMethodcontains a function handle to the set method.GetMethod包含获取方法的函数句柄。

Obtain these handles from themeta.property目的:

mc =?班级名称; mp = findobj(mc.PropertyList,'姓名',,,,'属性名称');fh = mp.getMethod;

例如,如果班级我的课defines a get method for itsText属性,您可以从meta.class目的:

mc =?myclass;mp = findobj(Mc.propertylist,'姓名',,,,'Text');fh = mp.getMethod;

返回的值,FH,包含针对指定类的指定属性名称定义的get方法的函数句柄。

For information on defining function handles, see创建功能句柄

设置并获得方法执行和属性事件

MATLAB software generates events before and after set and get operations. You can use these events to inform listeners that property values have been referenced or assigned. The timing of event generation is as follows:

  • PreGet- 在调用属性获取方法之前触发

  • PostGet- 在属性获取方法返回其值后触发

如果一类计算属性值(依赖= true),,,,then the behaviors of its set events are like the get events:

  • 预设- 在调用属性集方法之前触发

  • 邮局— Triggered after calling the property set method

如果未计算属性(Dependent = false,,,,the default), then the assignment statement with the set method generates the events:

  • 预设- 在设置方法中分配新属性值之前触发

  • 邮局— Triggered after assigning the new property value within the set method

有关使用属性事件的信息,请参阅创建属性侦听器

访问方法和包含数组的属性

您可以使用包含数组的属性来使用数组索引,而不会干扰属性集并获取方法。

用于索引参考:

val = obj。PREPNAME((n);

MATLAB calls the get method to get the referenced value.

For indexed assignment:

OBJ。PREPNAME((n) = val;

MATLAB:

  • Invokes the get method to get the property value

  • 在返回的属性上执行索引分配

  • 将新属性值传递给设定方法

访问方法和对象的数组

When reference or assignment occurs on an object array, MATLAB calls the set and get methods in a loop. In this loop, MATLAB always passes scalar objects to set and get methods.

用访问方法修改属性值

在您要在分配或返回属性值之前执行一些其他步骤的情况下,属性访问方法很有用。例如,测试点类使用属性集方法检查值的范围。然后,如果它在特定范围内,它将应用缩放,并将其设置为如果不是。

属性获取方法在返回其当前值之前应用了比例因子:

ClassDef测试点properties预期result = []结尾properties(常数)缩放法= 0.001结尾methodsfunctionobj = set.ExpectedResult(OBJ,ERIN)iferin> = 0 && erin <= 100 erin = erin。*obj.scalingfactor;obj.expedResult = erin;elseOBJ。expectedResult = NaN;结尾结尾functioner = get.expectedResult(obj) er = obj.expectedResult/obj.scalingFactor;结尾结尾结尾

Related Topics