Main Content

保持班级兼容性

重命名属性

假设您想重命名属性,但不想在参考原始属性的现有代码中引起错误。例如,重命名一个名为的公共财产OfficeNumber地点。这是原始类的定义:

ClassDef雇主properties名称电子邮件officenumber%重命名为位置结尾结尾

使用隐藏的依赖属性可以实现所需的结果。

  • 在课堂定义中,设置OfficeNumberproperty attributes toDependentandHidden

  • Create a property set method forOfficeNumberthat sets the value of the地点财产。

  • 创建属性获取方法OfficeNumberthat returns the value of the地点location property.

While theOfficeNumber属性是隐藏的,现有代码可以继续访问此属性。这Hiddenattribute 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;结尾结尾结尾

Saving and Loading雇主对象

您可以加载旧实例雇主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, theOfficeNumberproperty 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;结尾结尾结尾

Update Property When Loading

假设您修改类,以使属性值的形式或类型更改。加载时必须更新类的先前保存对象,以具有符合性的属性值。

Consider a class that has anAccountID财产。Suppose that all account numbers must migrate from eight-digit numeric values to 12-element character arrays.

您可以通过实施LoadObj方法。

LoadObjmethod:

  • Tests to determine if the加载功能通过a结构or object. AllLoadObj方法必须同时处理结构and object when there is an error in加载

  • Tests to determine if theAccountIDnumber contains eight digits. If so, change it to a 12-element character array by calling thepaddAccID方法。

更新后AccountID财产,LoadObj返回aMyAccountMATLAB加载到工作空间的对象。

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课堂使用技术的组合来保持与类的新版本的兼容性。

假设您定义一个类代表电话簿中的条目。这PhoneBookEntryclass defines three properties:姓名,,,,地址,,,,and电话号码

ClassDefPhoneBookEntryproperties名称地址Phonenumber结尾结尾

但是,在将来的版本中,该类添加了更多属性。为了提供灵活性,PhoneBookEntrysaves property data in a结构使用它saveobj方法。

methodsfunctions = saveobj(obj)s.name = obj.name;s.Address = obj.address;s.phonenumber = obj.phoneNumber;结尾结尾

LoadObjmethod creates thePhoneBookEntryobject, 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;结尾结尾结尾

Version 2 of thePhoneBookEntry班级

在版本2中PhoneBookEntry上课,你分开地址财产进入StreetAddress,,,,City,,,,状态,,,,and邮政编码properties.

通过这些更改,您无法在上一个版本中加载版本2对象。但是,版本2采用多种技术来启用兼容性:

  • 保留地址属性(版本1中使用)作为一个Dependentproperty 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;结尾结尾结尾结尾

Related Topics