主要内容

获取属性信息

元。属性对象

使用meta.property类以确定属性属性的值。a的可写性质meta.property对象对应于关联属性的属性。可写对象的值meta.property属性对应于类定义中指定的属性值。

得到了meta.property对象的属性meta.class对象。得到meta.class对象的类:

  • 使用元类函数作用于类的对象。

  • 使用使用类名的操作符。

例如,BasicHandle类定义了三个属性:

classdef BasicHandle < handle property (SetAccess = private) Date = Date PassKey = randi(9,[1,7]) end properties class {mustBeMember(Category,{'new','change'})} = 'new' end end . class . class . class . class . class . class . class . class . class . class . class . class . class . class . class . class

创建meta.class对象使用带有类名的操作符:

mc = ? BasicHandle
mc = class with properties: Name: 'BasicHandle' Description: " DetailedDescription: " Hidden: 0 Sealed: 0 Abstract: 0 Enumeration: 0 ConstructOnLoad: 0 handlecomcompatible: 1 inferorclasses: {0×1 cell} ContainingPackage: [0×0 meta. html][3×1 meta. package]方法列表:[22×1 meta. properties][1×1 meta. method]EnumerationMemberList: [0×1 meta. event]SuperclassList: [1×1 meta.class]

meta.class对象属性命名PropertyList包含meta.property对象,每个对象对应类定义的属性。属性关联的属性的名称meta.property对象元素1是:

mc.PropertyList (1) . name
ans =日期

meta.class对象包含一个meta.property对象的所有属性,包括隐藏属性。的属性函数仅返回公共属性。

对于句柄类,使用处理findprop方法获取meta.property对象的特定属性。

例如,找到meta.property对象的类别财产的BasicHandle类。

议员= findprop (BasicHandle,“类别”
mp = property with properties: Name: 'Category' Description: " DetailedDescription: " GetAccess: 'public' SetAccess: 'public' Dependent: 0 Constant: 0 Abstract: 0 Transient: 0 Hidden: 0 getobserable: 0 setobserable: 0 AbortSet: 0 NonCopyable: 0 GetMethod: [] SetMethod: [] HasDefault: 1 DefaultValue: 'new' DefiningClass: [1×1 meta.class]

前面的meta.property显示显示默认值BasicHandle对象类别属性:

  • 有公共GetAccessSetAccess

  • 默认值为

有关属性属性的列表,请参见属性表

如何索引元类对象

控件直接访问其他元类对象meta.class对象属性。例如,语句:

mc = ? containers.Map;

返回一个meta.class对象:

类(mc)
ans = meta.class

引用PropertyListmeta.class属性返回带有1的数组meta.property对象的每个属性容器。地图类:

类(mc.PropertyList)
ans = meta.property

每个数组元素都是单个的meta.property对象:

mc.Properties (1)
Ans = [1x1 meta.property]

的名字财产的meta.property对象包含一个字符Vector是属性的名称:

类(mc.PropertyList (1) . name)
ans =字符

MATLAB应用标准®为访问元类对象中的信息建立索引。

例如,meta.classPropertyList属性包含的数组meta.property对象。下面的表达式访问第一个meta.property对象,并返回第一个和最后一个(Ct)的信件字符包含在meta.property的名字财产。

mc.PropertyList (1) . name([1 end])
ans = Ct

如何找到具有特定属性的属性

这个例子实现了一个查找具有特定属性值的属性的函数。例如,你可以:

  • 查找定义常量属性的对象(常数属性设置为真正的).

  • 确定哪些属性是只读的(GetAccess =公共SetAccess =私人).

findAttrValue函数返回设置指定属性的属性名称的单元格数组。

findAttrValue函数使用以下技术访问元数据中的信息:

  • 如果输入参数,obj,是一个字符向量,使用meta.class.fromName的静态方法meta.class对象。

  • 如果输入参数,obj,是一个对象,使用元类函数来获取meta.class对象。

  • 每个属性都有一个关联meta.property对象。从meta.classPropertyList财产。

  • 使用处理findprop方法确定所请求的属性属性是否是有效的属性名称。属性的所有属性都是属性meta.property对象。声明,findobj (mp, PropertyName)决定meta.property对象,国会议员,具有一个名为PropertyName

  • 参考meta.property使用动态字段名的对象属性。例如,如果attrName =“常数”,用MATLAB对表达式进行转换议员(attrName)。mp。Constant

  • 可选的第三个参数允许指定不符合逻辑的属性的值真正的(如GetAccessSetAccess).

函数cl_out = findAttrValue (obj, attrName变长度输入宗量)如果ischar(obj) mc = meta.class.fromName(obj);elseifIsobject (obj) MC =元类(obj);结束2 = 0;numb_props =长度(mc.PropertyList);cl_array =细胞(1、numb_props);c = 1:numb_props mp = mc.PropertyList(c);如果isempty (findprop (mp, attrName))错误('不是有效的属性名'结束attrValue = mp。(attrName);如果attrValue如果islogical(attrValue) || strcmp(varargin{1},attrValue) ii = ii + 1;cl_array (ii) = {mp.Name};结束结束结束cl_out = cl_array (1: ii);结束

发现财产属性

假设您有以下内容容器。地图对象:

mapobj =容器。地图({“玫瑰”“自行车”},{“花”“机”});

发现属性私人SetAccess

findAttrValue (mapobj“SetAccess”“私人”
ans = 'Count' 'KeyType' 'ValueType' 'serialization'

发现属性公共GetAccess

findAttrValue (mapobj“GetAccess”“公共”
ans = 'Count' 'KeyType' 'ValueType'

相关的话题