假设您想重命名属性,但不想在参考原始属性的现有代码中引起错误。例如,重命名一个名为的公共财产OfficeNumber
至地点
。这是原始类的定义:
ClassDef雇主properties名称电子邮件officenumber%重命名为位置结尾结尾
使用隐藏的依赖属性可以实现所需的结果。
在课堂定义中,设置OfficeNumber
property attributes toDependent
andHidden
。
Create a property set method forOfficeNumber
that sets the value of the地点
财产。
创建属性获取方法OfficeNumber
that returns the value of the地点
location property.
While theOfficeNumber
属性是隐藏的,现有代码可以继续访问此属性。这Hidden
attribute does not affect access.
因为OfficeNumber
是依赖的,通过添加新属性,不需要存储的冗余。MATLAB®不存储或保存相关属性。
Here is the updated class definition.
ClassDef雇主properties名称电子邮件位置结尾properties(Dependent, Hidden) OfficeNumber结尾methodsfunctionobj = set.officeNumber(obj,val)obj.location = val;结尾functionval = get.officeNumber(obj)val = obj.location;结尾结尾结尾
雇主
对象您可以加载旧实例雇主
class in the presence of the new class version. Code that refers to theOfficeNumber
财产继续工作。
Suppose that you want to be able to load new雇主
objects into systems that still have the old version of the雇主
班级。与旧版本和新版本的兼容性:
Define theOfficeNumber
属性为Hidden
, 但不是Dependent
。
Define the地点
属性为Dependent
。
在这个版本的雇主
class, theOfficeNumber
property saves the value used by the地点
财产。加载对象分配三个原始属性的值(姓名
,,,,电子邮件
,,,,andOfficeNumber
), but does not assign a value to the new地点
财产。这lack of the地点
旧类定义中的属性不是问题。
ClassDef雇主properties姓名电子邮件结尾properties(Dependent) Location结尾properties(隐藏)结尾methodsfunctionobj = set.location(obj,val)obj.officeNumber = val;结尾functionval = get.location(obj)val = obj.officeNumber;结尾结尾结尾
假设您修改类,以使属性值的形式或类型更改。加载时必须更新类的先前保存对象,以具有符合性的属性值。
Consider a class that has anAccountID
财产。Suppose that all account numbers must migrate from eight-digit numeric values to 12-element character arrays.
您可以通过实施LoadObj
方法。
这LoadObj
method:
Tests to determine if the加载
功能通过a结构
or object. AllLoadObj
方法必须同时处理结构
and object when there is an error in加载
。
Tests to determine if theAccountID
number contains eight digits. If so, change it to a 12-element character array by calling thepaddAccID
方法。
更新后AccountID
财产,LoadObj
返回aMyAccount
MATLAB加载到工作空间的对象。
ClassDefMyAccountpropertiesAccountID结尾methodsfunctionobj = padaccid(obj)ac = obj.accountid;acstr = num2str(ac);iflength(acstr) < 12 obj.AccountID = [acstr,repmat('0',1,12长度(ACSTR))];结尾结尾结尾methods(Static)functionobj = loadObj(a)ifisstruct(a) obj = MyAccount; obj.AccountID = a.AccountID; obj = padAccID(obj);Elseifisa(a,'MyAccount')obj = padaccid(a);结尾结尾结尾结尾
您不需要实施saveobj
方法。您正在使用LoadObj
只是为了确保在加载时提出旧的保存对象。
这PhoneBookEntry
课堂使用技术的组合来保持与类的新版本的兼容性。
假设您定义一个类代表电话簿中的条目。这PhoneBookEntry
class defines three properties:姓名
,,,,地址
,,,,and电话号码
。
ClassDefPhoneBookEntryproperties名称地址Phonenumber结尾结尾
但是,在将来的版本中,该类添加了更多属性。为了提供灵活性,PhoneBookEntry
saves property data in a结构
使用它saveobj
方法。
methodsfunctions = saveobj(obj)s.name = obj.name;s.Address = obj.address;s.phonenumber = obj.phoneNumber;结尾结尾
这LoadObj
method creates thePhoneBookEntry
object, which is then loaded into the workspace.
方法(静态)functionobj = loadObj(s)ifisstruct(s) newObj = PhoneBookEntry; newObj.Name = s.Name; newObj.Address = s.Address; newObj.PhoneNumber = s.PhoneNumber; obj = newObj;elseobj = s;结尾结尾结尾
PhoneBookEntry
班级在版本2中PhoneBookEntry
上课,你分开地址
财产进入StreetAddress
,,,,City
,,,,状态
,,,,and邮政编码
properties.
通过这些更改,您无法在上一个版本中加载版本2对象。但是,版本2采用多种技术来启用兼容性:
保留地址
属性(版本1中使用)作为一个Dependent
property with privatesetAccess
。
Define an地址
property get method (get.address
)建造一个char
与版本2兼容的向量地址
财产。
这saveobj
方法调用get.address
将对象数据分配给一个的方法结构
这与以前的版本兼容。这结构
继续只有一个地址
field built from the data in the newStreetAddress
,,,,City
,,,,状态
,,,,and邮政编码
properties.
When theLoadObj
方法设置地址
属性,它调用属性集方法(set.Address
), which extracts the substrings required by theStreetAddress
,,,,City
,,,,状态
,,,,and邮政编码
properties.
这短暂的
(未保存)属性SaveInoldFormat
使您可以指定是否将版本2对象保存为一个结构
或对象。
ClassDefPhoneBookEntryproperties名称StreetAddress City State Zipcode Phonenumber结尾properties(常数)sep =', '结尾properties(Dependent, SetAccess=private) Address结尾properties(瞬态)SaveInoldFormat = false;结尾methods(Static)functionobj = loadObj(s)ifisstruct(s) obj = PhoneBookEntry; obj.Name = s.Name; obj.Address = s.Address; obj.PhoneNumber = s.PhoneNumber;elseobj = s;结尾结尾结尾methodsfunctionaddress = get.Address(obj) address = [obj.StreetAddress,obj.Sep,obj.City,obj.Sep,...obj.State,obj.Sep,obj.ZipCode];结尾functionobj = set.Address(obj,address) addressItems = regexp(address,obj.Sep,'分裂');if长度(addressItems) = = 4 obj。StreetAddress = addressItems{1}; obj.City = addressItems{2}; obj.State = addressItems{3}; obj.ZipCode = addressItems{4};elseerror('PhoneBookEntry:InvalidAddressFormat',,,,...“无效的地址格式。”);结尾结尾functions = saveobj(obj)ifobj.saveinoldformat s.name = obj.name;s.Address = obj.address;s.phonenumber = obj.phoneNumber;结尾结尾结尾结尾