分享

Passing Arguments to Constructors During Load

 windycityboy 2014-07-08


Calling Constructors When Loading Objects

You can set the class ConstructOnLoad attribute when you need to call the default (no argument) class constructor on an object that is loaded from a MAT-file. Then load automatically calls the object's class constructor, but cannot pass any arguments to it.

If the object you are loading requires a call to its class constructor and this call requires you to pass arguments to the constructor, you can implement a loadobj method that performs this task. For example, suppose the object's constructor adds a listener and, therefore, must be passed a handle to the object triggering the event (required by the addlistener handle class method) to create this listener. Yourloadobj method could call the constructor with the required argument.

Code for This Example

The following information on saving and loading objects refers to a BankAccountSL class. Click the following link to open the full code for this class in the MATLAB? editor:

Open class definition in editor

Example Overview

This example shows how to use loadobj to call a class constructor with arguments at load time. Because the constructor requires arguments, you cannot use the ConstructOnLoad attribute to load the object, which causes a call to the default (no arguments) constructor.

This example uses loadobj to determine the status of a BankAccountSL object when the object data is loaded, and then calls the class constructor with the appropriate arguments to create the object. This approach provides a way to modify the criteria for determining status over time, while ensuring that all loaded objects are using the current criteria.

The saveobj method extracts the data from the object and writes this data into a struct, which saveobj returns to the save function.

Saving Only Object Data with saveobj

The following saveobj method saves the values of the BankAccountSL object's AccountNumber and AccountBalance properties in thestruct variable A, which has field names that match the property names. saveobj then returns the variable A to be saved in the MAT-file by the save function.

methods 
   function A = saveobj(obj)
      A.AccountNumber = obj.AccountNumber;
      A.AccountBalance = obj.AccountBalance;
   end
end

Reconstructing Objects with loadobj

The BankAccountSL class AccountStatus property is Transient because its value depends on the value of the AccountBalanceproperty and the current criteria and possible status values. You can use the loadobj method to update all saved BankAccount objects when they are loaded into your system.

To create a valid object, loadobj calls the constructor using the data saved in the struct A and passes any other required arguments.

If the account balance is greater than zero, AccountStatus is set to open. If the account balance is zero or less, AccountStatus is set tooverdrawn or to frozen.

The following loadobj method calls the class constructor with the appropriate values for the arguments:

methods (Static)
   function obj = loadobj(A)
      if A.AccountBalance > 0
         obj = BankAccountSL(A.AccountNumber,A.AccountBalance,'open');
      elseif  A.AccountBalance < 0) && (A.AccountBalance >= -100)
         obj = BankAccountSL(A.AccountNumber,A.AccountBalance,'overdrawn');
      else
         obj = BankAccountSL(A.AccountNumber,A.AccountBalance,'frozen');
      end
   end
end

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多