Calling Constructors When Loading ObjectsYou 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 ExampleThe 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: Example OverviewThis 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 saveobjThe 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 loadobjThe 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 |
|
来自: windycityboy > 《class》