logo资料库

Matlab面向对象编程.pdf

第1页 / 共503页
第2页 / 共503页
第3页 / 共503页
第4页 / 共503页
第5页 / 共503页
第6页 / 共503页
第7页 / 共503页
第8页 / 共503页
资料共503页,剩余部分请下载后查看
toc
Using Object-Oriented Design in MATLAB
Where to Begin
Video Demo of MATLAB Classes
MATLAB Programmer Without Object-Oriented Programming Experience
MATLAB Programmer with Object-Oriented Programming Experience
Why Use Object-Oriented Design
Approaches to Writing MATLAB Programs
Procedural Program Design
Object-Oriented Program Design
Classes and Objects
Using Objects in MATLAB Programs
Objects Organize Data
Objects Manage Their Own Data
When Should You Start Creating Object-Oriented Programs
Understanding a Problem in Terms of Its Objects
Objects Manage Internal State
Reducing Redundancy
Defining Consistent Interfaces
Reducing Complexity
Fostering Modularity
Overloaded Functions and Operators
Reduce Code Redundancy
Learning More
Class Diagrams Used in This Documentation
MATLAB Classes Overview
MATLAB Classes
Classes in the MATLAB Language
User-Defined Classes
MATLAB Classes — Key Terms
Some Basic Relationships
Classes
Class Hierarchies
Reusing Solutions
Objects
Encapsulating Information
Examples to Get Started
Learning Object-Oriented Programming
Detailed Information and Examples
Rapid Access to Information
Developing Classes — Typical Workflow
Formulating a Class
Defining Class Data
Defining Class Operations
Defining Class Events
Implementing the BankAccount Class
Display Fully Commented Example Code
Class Definition
Implementing the AccountManager Class
Class Definition
Using the BankAccount Class
Using Objects to Write Data to a File
Flexible Workflow
Performing a Task with an Object
The Filewriter Class
Using a Filewriter Object
Using Objects in Functions
More Information on These Techniques
Example — Representing Structured Data
Display Fully Commented Example Code
Objects As Data Structures
Concepts on Which This Example Is Based.
Structure of the Data
Defining the TensileData Class
Creating an Instance and Assigning Data
Advantages of a Class vs. a Structure Array
Restricting Properties to Specific Values
Defining the Material Property Set Function
Simplifying the Interface with a Constructor
Calculating Modulus
Dependent Properties
Modulus Property Get Method
Modulus Property Set Method
Displaying TensileData Objects
A Method to Plot Stress vs. Strain
Example — Implementing Linked Lists
Displaying Fully Commented Example Code
Important Concepts Demonstrated
Encapsulation
Handle Class Behavior
dlnode Class Design
Class Properties
Class Methods
Creating Doubly Linked Lists
Why a Handle Class for Doubly Linked Lists?
Defining the dlnode Class
Class Properties
Creating a Node Object
Disconnecting Nodes
Inserting Nodes
Displaying a Node on the Command Line
Deleting a Node Object
Specializing the dlnode Class
Using the NamedNode Class to Create a Doubly Linked List
Example — Class for Graphing Functions
Display Fully Commented Example Code
Class Definition Block
Using the topo Class
Behavior of the Handle Class
How a Value Class Differs
Class Definition—Syntax Reference
Class Folders
Options for Class Folders
Creating a Single, Self-Contained Class Definition File
Distributing the Class Definition to Mulitple Files
Grouping Classes with Package Folders
More Information on Class Folders
Class Components
Class Building Blocks
More In Depth Information
The Classdef Block
Specifying Attributes and Superclasses
Assigning Class Attributes
Specifying Superclasses
Specifying Properties
What You Can Define
How to Initialize Property Values
Defining Default Values
Assigning Property Values from Within the Constructor
Initializing Properties to Unique Values
Property Attributes
Property Access Methods
Referencing Object Properties Using Variables
Specifying Methods and Functions
The Methods Block
Method Calling Syntax
Methods In Separate Files
Define the Method Like Any Function
Methods That Must Be In the classdef File
Specify Method Attributes in classdef File
Defining Static Methods in Separate Files
For an Example
Defining Private Methods
More Detailed Information On Methods
Defining Class-Related Functions
Events and Listeners
Specifying Events
Listening for Events
Specifying Attributes
Attribute Syntax
Attribute Descriptions
Specifying Attribute Values
Simpler Syntax for true/false Attributes
Calling Superclass Methods on Subclass Objects
Calling a Superclass Constructor
Calling Superclass Methods
A Class Code Listing
Example of Class Definition Syntax
Understanding Code Analyzer Warnings
Syntax Warnings and Property Names
Warnings Caused by Variable/Property Name Conflicts
Exception to Variable/Property Name Rule
Functions Used with Objects
Functions to Query Class Members
Functions to Test Objects
Using the Editor and Debugger with Classes
Referring to Class Files
Debugging Class Files
Modifying and Reloading Classes
Ensuring MATLAB Uses Your Changes
Clear Class Instances
Clear Classes
Places That Can Hold Instances
Compatibility with Previous Versions
New Class-Definition Syntax Introduced with MATLAB Software Vers
Cannot Mix Class Hierarchy
Only One @-Folder per Class
Private Methods
Changes to Class Constructors
Example of Old and New Syntax
New Features Introduced with Version 7.6
Examples of Old and New
Obsolete Documentation
MATLAB and Other OO Languages
Some Differences from C++ and Sun Java Code
Public Properties
Passing Objects to Functions
No Implicit Parameters
Dispatching
Calling Superclass Method
Other Differences
Common Object-Oriented Techniques
Defining and Organizing Classes
Class Overview
MATLAB User-Defined Classes
Attributes for Class Members
Kinds of Classes
Constructing Objects
Creating Class Hierarchies
Defining Classes — Syntax
classdef Syntax
Examples of Class Definitions
Class Attributes
Table of Class Attributes
Specifying Attributes
Superclass Attributes Are Not Inherited
Attribute Syntax
Using Expressions in Class Definitions
Basic Knowledge
Where Can You Use Expressions
Expressions in Attribute Specifications
Expressions in Default Property Specifications
Expressions in Class Methods
How MATLAB Evaluates Expressions
When Does MATLAB Evaluate These Expressions
Organizing Classes in Folders
Options for Class Folders
@-Folders
Path Folders
Access to Functions Defined in Private Folders
Class Precedence and MATLAB Path
Previous Behavior of Classes Defined in @-Folders
Specifying Class Precedence
InferiorClasses Attribute
Dominant Class
More Information
No Attribute Inheritance
Scoping Classes with Packages
Package Folders
Listing the Contents of a Package
Referencing Package Members Within Packages
Referencing Package Members from Outside the Package
Accessing Class Members — Various Scenarios
Packages and the MATLAB Path
Resolving Redundant Names
Package Functions vs. Static Methods
Importing Classes
Related Information
Syntax for Importing Classes
Importing Package Functions
Package Function and Class Method Name Conflict
Clearing Import List
Value or Handle Class — Which to Use
Comparing Handle and Value Classes
Why Select Handle or Value
Behavior of MATLAB Built-In Classes
Behavior of User-Defined Classes
Value Classes
Value Class Behavior
Creating a Value Class
Handle Classes
Creating a Handle Class
Subclasses of Handle Classes
Handle Class Behavior
Initializing Properties to Handle Objects
employee as a Value Class
Deleting Handles
Which Kind of Class to Use
Examples of Value and Handle Classes
When to Use Handle Classes
When to Use Value Classes
The Handle Superclass
Building on the Handle Class
Handle Subclasses
Handle Class Methods
Relational Methods
Testing Handle Validity
Determining If a Handle Belongs to the Handle Class
Handle Class Delete Methods
When to Define a Delete Method for Your Class
Object Lifecycle
Sequence During Handle Object Destruction
Restricting Object Deletion
Deleting Subclass Objects
Finding Handle Objects and Properties
Finding Handle Objects
Finding Handle Object Properties
Implementing a Set/Get Interface for Properties
The Standard Set/Get Interface
Property Get Method
Property Set Method
Subclassing hgsetget
Listing All Properties
Customizing the Property List
Controlling the Number of Instances
Limiting Instances
Implementing a Singleton Class
Properties — Storing Class Data
How to Use Properties
What Are Properties
Flexibility of Object Properties
Types of Properties
Features of Stored Properties
When to Use Stored Properties
Features of Dependent Properties
When to Use Dependent Properties
Defining Properties
Property Definition Block
Assigning a Default Value
Accessing Property Values
Inheritance of Properties
Specifying Property Attributes
Property Attributes
Table of Property Attributes
Controlling Property Access
Property Access Methods
Restrictions on Access Methods
Access Methods Cannot Call Other Functions to Access Property Va
Defining Access Methods
Property Set Methods
Set Method Behavior
Property Get Methods
Dependent Properties — Values Not Stored
When to Use Set Methods with Dependent Properties
When to Use Private Set Access with Dependent Properties
Set and Get Method Execution and Property Events
Access Methods and Subscripted Reference and Assignment
Performing Additional Steps with Property Access Methods
Dynamic Properties — Adding Properties to an Instance
What Are Dynamic Properties
Characteristics of Dynamic Properties
Defining Dynamic Properties
Setting Dynamic Property Attributes
Assigning Data to the Dynamic Property
Responding to Dynamic-Property Events
Triggering the PropertyAdded Event
Dynamic Properties and Ordinary Property Events
Defining Property Access Methods for Dynamic Properties
Dynamic Properties and ConstructOnLoad
Methods — Defining Class Operations
Class Methods
What Are Methods
Kinds of Methods
Method Attributes
Table of Method Attributes
Ordinary Methods
Defining Methods
Methods Inside classdef Block
Methods in Separate Files
Determining Which Method Is Invoked
Dominant Argument
Dot Notation vs. Function Notation
Referencing Names with Expressions—Dynamic Reference
Specifying Precedence
Controlling Access to Methods
Invoking Superclass Methods in Subclass Methods
Limitations of Use
Invoking Built-In Methods
Class Constructor Methods
Rules for Constructors
Examples of Class Constructors
Initializing the Object Within a Constructor
Referencing the Object in a Constructor
Supporting the No Input Argument Case
Constructing Subclasses
Reference Only Specified Superclasses
Make No Conditional Calls to Superclass Constructors
Zero or More Superclass Arguments
More on Subclasses
Errors During Class Construction
Basic Structure of Constructor Methods
Static Methods
Why Define Static Methods
Defining a Static Method
Calling Static Methods
Inheriting Static Methods
Overloading Functions for Your Class
Overloading MATLAB Functions
Using MATLAB Functions in Conversion Methods
Implementing MATLAB Operators
Rules for Naming to Avoid Conflicts
Object Precedence in Expressions Using Operators
Specifying Precedence of User-Defined Classes
Location in the Hierarchy
Class Methods for Graphics Callbacks
Callback Arguments
Background Information
General Syntax for Callbacks
Object Scope and Anonymous Functions
Using Value Classes
Using Handle Classes
Example — Class Method as a Slider Callback
Displaying the Class Files
Class Properties
Class Constructor
Using the SeaLevelAdjuster Class
Object Arrays
Information About Arrays
Basic Knowledge
Creating Object Arrays
Building Arrays in the Constructor
Initializing Arrays of Value Objects
Initial Values of Object Array Properties
Creating Empty Arrays
Assigning Values to an Empty Array
Arrays of Handle Objects
Initializing a Handle Object Array
Concatenating Objects of Different Classes
Basic Knowledge
MATLAB Concatenation Rules
Concatenating Objects
Modifying Default Concatenation
Converting to the Dominant Class
Calling the Dominant-Class Constructor
Implementing Converter Methods
Events — Sending and Responding to Messages
Learning to Use Events and Listeners
What You Can Do With Events and Listeners
Some Basic Examples
Quick Overview
Simple Event Listener Example
Defining and Triggering an Event
Defining the Event Data
Creating a Listener for the Overflow Event
Responding to a Button Click
The PushButton Class
The AxesObj Class
Creating a Listener for the Property Event
Events and Listeners — Concepts
The Event Model
Default Event Data
Customizing Event Data
Events Only in Handle Classes
Property-Set and Query Events
Listeners
Listener Order of Execution
Event Attributes
Table of Event Attributes
Defining Events and Listeners — Syntax and Techniques
Naming Events
Triggering Events
Listening to Events
Removing Listeners
Defining Event-Specific Data
Ways to Create Listeners
Attach Listener to Event Source — Using addlistener
Limiting Listener Scope — Constructing event.listener Objects Di
Temporarily Deactivating Listeners
Permanently Deleting Listeners
Defining Listener Callback Functions
Adding Arguments to a Callback Function
Listening for Changes to Property Values
Creating Property Listeners
Set Property Attributes to Enable Property Events
Define a Callback Function for the Property Event
Add a Listener to the Property
Example Property Event and Listener Classes
Class Generating the Event
Class Defining the Listeners
Aborting Set When Value Does Not Change
How AbortSet Works
Example — Using Events to Update Graphs
Example Overview
Access Fully Commented Example Code
Techniques Demonstrated in This Example
Summary of fcneval Class
Summary of fcnview Class
Methods Inherited from Handle Class
Using the fcneval and fcnview Classes
Implementing the UpdateGraph Event and Listener
Defining and Firing the UpdateGraph Event
Determining When to Trigger the Event
Determining Suitability of the Expression
Other Approaches
Defining the Listener and Callback for the UpdateGraph Event
Implementing the PostSet Property Event and Listener
Sequence During the Lm Property Assignment
Enabling the PostSet Property Event
Defining the Listener and Callback for the PostSet Event
Enabling and Disabling the Listeners
Context Menu Callback
Building on Other Classes
Hierarchies of Classes — Concepts
Classification
Developing the Abstraction
Designing Class Hierarchies
Super and Subclass Behavior
A Subclass Object Is A Superclass Object
Treat Subclass Objects Like Superclass Objects
Limitations to Object Substitution
Implementation and Interface Inheritance
Creating Subclasses — Syntax and Techniques
Defining a Subclass
Class Attributes
Referencing Superclasses from Subclasses
Referencing Superclasses Contained in Packages
Initializing Objects When Using Multiple Superclasses
Constructor Arguments and Object Initialization
Call Only Direct Superclass from Constructor
Sequence of Constructor Calls in a Class Hierarchy
Using a Subclass to Create an Alias for an Existing Class
Old Class Constructor Requires Arguments
Modifying Superclass Methods and Properties
Modifying Superclass Methods
Extending Superclass Methods
Completing Superclass Methods
Redefining Superclass Methods
Modifying Superclass Properties
Private Local Property Takes Precedence in Method
Subclassing Multiple Classes
Class Member Compatibility
Property Conflicts
Method Conflicts
Event Conflicts
Using Multiple Inheritance
Subclassing MATLAB Built-In Classes
MATLAB Built-In Classes
Why Subclass Built-In Classes
Which Functions Work With Subclasses of Built-Ins
Built-In Classes You Cannot Subclass
Examples of Subclasses of Built-In Classes
Behavior of Built-In Functions with Subclass Objects
Behavior Categories
Subclasses That Define Properties
More information on Built-In Methods
Extending the Operations of a Built-In Class
Built-In Methods That Operate on Data Values
Built-In Methods That Operate on Data Organization
Indexing Methods
Concatenation Functions
Example — A Class to Manage uint8 Data
Using the DocUint8 Class
Indexing Operations
Concatenation Operations
Data Operations
Example — Adding Properties to a Built-In Subclass
Methods Implemented
Property Added
Subclass with Properties
Indexed Reference of a DocExtendDouble Object
Concatenating DocExtendDouble Objects
Understanding size and numel
Subclass Inherited Behavior
Classes Not Derived from Built-In Classes
Overloading size
Avoid Overloading numel
Example — A Class to Represent Hardware
Why Derive from int32
Class Definition
Using the Class with Methods of int32
Abstract Classes and Interfaces
Abstract Classes
Defining Abstract Classes
Abstract Properties
Abstract Methods
Interfaces and Abstract Classes
Example — Interface for Classes Implementing Graphs
Interface Properties and Methods
Interface Guides Class Design
Defining the Interface
Method to Work with All Subclasses
Deriving a Concrete Class — linegraph
Adding Properties
The linegraph Constructor
Implementing the draw Method
Implementing the zoom Method
Defining the Property Set Methods
Using the linegraph Class
Saving and Loading Objects
The Save and Load Process
The Default Save and Load Process
What Information Is Saved
Loading Property Data
Errors During Load
Saving and Loading Deleted Handle Objects
saveobj and loadobj
When to Modify Object Saving and Loading
Why Implement saveobj and loadobj
Information to Consider
Modifying the Save and Load Process
Class saveobj and loadobj Methods
Implement loadobj as a Static Method
Processing Objects During Load
Updating an Object Property When Loading
Save and Load Applications
Example — Maintaining Class Compatibility
Versions of a Phone Book Application Program
Version 1 — Stores Data in struct
Version 2 — Maps struct Fields to Object Properties
Version 3 — Adds More Properties to Class
Passing Arguments to Constructors During Load
Calling Constructors When Loading Objects
Code for This Example
Example Overview
Saving Only Object Data with saveobj
Reconstructing Objects with loadobj
Saving and Loading Objects from Class Hierarchies
Saving and Loading Subclass Objects
Reconstructing the Subclass Object from a Saved Struct
Saving and Loading Dynamic Properties
Reconstructing Objects That Have Dynamic Properties
Why You Need saveobj and loadobj Methods
Implementing the saveobj and loadobj Methods
Tips for Saving and Loading
Using Default Property Values to Reduce Storage
Reducing Object Storage
Implementing Forward and Backward Compatibility
Avoiding Property Initialization Order Dependency
Controlling Property Loading
When to Use Transient Properties
Calling Constructor When Loading
Referring to Constant Values
Defining Named Constants
A Class to Define Named Constants
Assigning Values to Constant Properties
Referencing Constant Properties
A Package for Constants
Setting Constant Property Default
Obtaining Information About Classes from Meta-Classes
Working with Meta-Classes
What Are Meta-Classes?
The meta Package
Creating Meta-Class Objects
Using Meta-Class Objects
Using Meta-Classes to Inspect Classes and Objects
Inspecting a Class
Inspecting the Class Definition
Inspecting Properties
Inspecting an Instance of a Class
Finding Objects Having Specific Settings
Finding Handle Objects
Finding Property/Value Pairs
Finding Objects with Specific Property Names
Using Logical Expressions
Perusing Meta-Classes with findobj
Getting Information About Properties
Information Contained in the meta.property object
Indexing Meta-Class Objects
Example — Finding Properties with Specific Attributes
Finding Property Attributes
Getting Property Default Values
Property Default Values
Querying a Default Value
Default Values Defined as Expressions
Property With No Explicit Default Value
Abstract Property
Property With Expression That Errors
Property With Explicitly Defined Default Value
Specializing Object Behavior
Methods That Modify Default Behavior
How to Modify Behavior
Which Methods Control Which Behaviors
When to Overload MATLAB Functions
Example of Modified Behavior
Caution When Overloading MATLAB Functions
Interactions with numel and Overloaded subsref and subsasgn
Ensuring MATLAB Calls Your Overloaded Method
Redefining Concatenation for Your Class
Default Concatenation
Example of horzcat and vertcat
Displaying Objects in the Command Window
Default Display
Examples of disp Methods
Relationship Between disp and display
Override disp Or disp and display
Converting Objects to Another Class
Why Implement a Converter
Converters and Subscripted Assignment
Examples of Converter Methods
Indexed Reference and Assignment
Overview
Default Indexed Reference and Assignment
What You Can Modify
When to Modify Indexing Behavior
subsref and subsasgn Within Class Methods — Built-In Called
Understanding Indexed Reference
Complex Indexed References
Writing subsref
Examples of subsref
Avoid Overriding Access Attributes
Understanding Indexed Assignment
Indexed Assignment to Objects
Complex Indexed Assignments
A Class with Modified Indexing
Class Description
Specialized Subscripted Reference — subsref
Specialized Subscripted Assignment — subsasgn
Implementing Addition for Object Data — plus
Defining end Indexing for an Object
The end Method for the MyDataClass Example
Using Objects as Indices
Scenarios for Implementing Objects as Indices
Implementing subsindex
Implementing Operators for Your Class
Overloading Operators
Object Precedence
Examples of Overloaded Operators
MATLAB Operators and Associated Functions
Implementing a Class for Polynomials
Example — A Polynomial Class
Adding a Polynomial Object to the MATLAB Language
Displaying the Class Files
Summary of the DocPolynom Class
Using the DocPolynom Class
The DocPolynom Constructor Method
Constructor Calling Syntax
Removing Irrelevant Coefficients
Converting DocPolynom Objects to Other Types
The DocPolynom to Double Converter
The DocPolynom to Character Converter
Evaluating the Output
The DocPolynom disp Method
When MATLAB Calls the disp Method
The DocPolynom subsref Method
Special Behavior Requires Specializing subsref
subsref Implementation Details
Defining Arithmetic Operators for DocPolynom
Defining the + Operator
Defining the - Operator
Defining the * Operator
Using the Arithmetic Operators
Overloading MATLAB Functions for the DocPolynom Class
Defining the roots Function for the DocPolynom Class
Defining the polyval Function for the DocPolynom Class
Defining the diff Function for the DocPolynom Class
Defining the plot Function for the DocPolynom Class
Designing Related Classes
Example — A Simple Class Hierarchy
Shared and Specialized Properties
Designing a Class for Financial Assets
Displaying the Class Files
Summary of the DocAsset Class
The DocAsset Constructor Method
Property Definition Block
Constructor Method Code
Set Function for Type Property
The DocAsset Display Method
Designing a Class for Stock Assets
Displaying the Class Files
Summary of the DocStock Class
Specifying the Base Class
Property Definition Block
Using the DocStock Class
The DocStock Constructor Method
The DocStock disp Method
Designing a Class for Bond Assets
Displaying the Class Files
Summary of the DocBond Class
Specifying the Base Class
Property Definition Block
Using the DocBond Class
The DocBond Constructor Method
The calc_value Method
The DocBond disp Method
Designing a Class for Savings Assets
Displaying the Class Files
Summary of the DocSavings Class
Specifying the Base Class
Property Definition Block
Using the DocSavings Class
The DocSavings Constructor Method
The DocSavings disp Method
Example — Containing Assets in a Portfolio
Kinds of Containment
Designing the DocPortfolio Class
Displaying the Class Files
Summary of the DocPortfolio Class
Property Definition Block
How Class Properties Are Used
Using the DocPortfolio Class
The DocPortfolio Constructor Method
The DocPortfolio disp Method
The DocPortfolio pie3 Method
Visualizing a Portfolio
Index
tables
DocPolynom Class Properties
DocPolynom Class Methods
DocAsset Class Properties
DocAsset Class Methods
DocStock Class Properties
DocStock Class Methods
DocBond Class Properties
DocBond Class Methods
DocSavings Class Properties
DocSavings Class Methods
DocPortfolio Class Properties
DocBond Class Methods
MATLAB® 7 Object-Oriented Programming
How to Contact The MathWorks Web Newsgroup www.mathworks.com comp.soft-sys.matlab www.mathworks.com/contact_TS.html Technical Support suggest@mathworks.com bugs@mathworks.com doc@mathworks.com service@mathworks.com info@mathworks.com 508-647-7000 (Phone) Product enhancement suggestions Bug reports Documentation error reports Order status, license renewals, passcodes Sales, pricing, and general information 508-647-7001 (Fax) The MathWorks, Inc. 3 Apple Hill Drive Natick, MA 01760-2098 For contact information about worldwide offices, see the MathWorks Web site. Object-Oriented Programming © COPYRIGHT 1984–2010 by The MathWorks, Inc. The software described in this document is furnished under a license agreement. The software may be used or copied only under the terms of the license agreement. No part of this manual may be photocopied or reproduced in any form without prior written consent from The MathWorks, Inc. FEDERAL ACQUISITION: This provision applies to all acquisitions of the Program and Documentation by, for, or through the federal government of the United States. By accepting delivery of the Program or Documentation, the government hereby agrees that this software or documentation qualifies as commercial computer software or commercial computer software documentation as such terms are used or defined in FAR 12.212, DFARS Part 227.72, and DFARS 252.227-7014. Accordingly, the terms and conditions of this Agreement and only those rights specified in this Agreement, shall pertain to and govern the use, modification, reproduction, release, performance, display, and disclosure of the Program and Documentation by the federal government (or other entity acquiring for or through the federal government) and shall supersede any conflicting contractual terms or conditions. If this License fails to meet the government’s needs or is inconsistent in any respect with federal procurement law, the government agrees to return the Program and Documentation, unused, to The MathWorks, Inc. Trademarks MATLAB and Simulink are registered trademarks of The MathWorks, Inc. See www.mathworks.com/trademarks for a list of additional trademarks. Other product or brand names may be trademarks or registered trademarks of their respective holders. Patents The MathWorks products are protected by one or more U.S. patents. Please see www.mathworks.com/patents for more information. Revision History Online only March 2008 Online only October 2008 Online only March 2009 September 2009 Online only March 2010 Online only New for MATLAB 7.6 (Release 2008a) Revised for MATLAB 7.7 (Release 2008b) Revised for MATLAB 7.8 (Release 2009a) Revised for MATLAB 7.9 (Release 2009b) Revised for MATLAB 7.10 (Release 2010a)
1 2 Using Object-Oriented Design in MATLAB Where to Begin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Video Demo of MATLAB Classes . . . . . . . . . . . . . . . . . . . . . MATLAB Programmer Without Object-Oriented Programming Experience . . . . . . . . . . . . . . . . . . . . . . . . . MATLAB Programmer with Object-Oriented Programming Experience . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Why Use Object-Oriented Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Approaches to Writing MATLAB Programs When Should You Start Creating Object-Oriented 1-2 1-2 1-2 1-2 1-4 1-4 1-8 Class Diagrams Used in This Documentation . . . . . . . . . 1-17 MATLAB Classes Overview MATLAB Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Classes in the MATLAB Language . . . . . . . . . . . . . . . . . . . Some Basic Relationships . . . . . . . . . . . . . . . . . . . . . . . . . . . Examples to Get Started . . . . . . . . . . . . . . . . . . . . . . . . . . . . Learning Object-Oriented Programming . . . . . . . . . . . . . . . Detailed Information and Examples . . . . . . . . . . . . . . . . . Rapid Access to Information . . . . . . . . . . . . . . . . . . . . . . . . . 2-2 2-2 2-4 2-6 2-7 2-8 2-8 Developing Classes — Typical Workflow . . . . . . . . . . . . . Formulating a Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Implementing the BankAccount Class . . . . . . . . . . . . . Implementing the AccountManager Class 2-11 2-11 2-13 2-15 Contents iii
Using the BankAccount Class . . . . . . . . . . . . . . . . . . . . . . . 2-15 Using Objects to Write Data to a File . . . . . . . . . . . . . . . . Flexible Workflow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Performing a Task with an Object . . . . . . . . . . . . . . . . . . . . Using Objects in Functions . . . . . . . . . . . . . . . . . . . . . . . . . . Example — Representing Structured Data . . . . . . . . . . . Display Fully Commented Example Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Objects As Data Structures Structure of the Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Defining the TensileData Class . . . . . . . . . . . . . . . . . . . . . . Creating an Instance and Assigning Data . . . . . . . . . . . . . . Restricting Properties to Specific Values . . . . . . . . . . . . . . . Simplifying the Interface with a Constructor . . . . . . . . . . . Dependent Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Displaying TensileData Objects . . . . . . . . . . . . . . . . . . . . . . A Method to Plot Stress vs. Strain . . . . . . . . . . . . . . . . . . . . Example — Implementing Linked Lists . . . . . . . . . . . . . . Displaying Fully Commented Example Code . . . . . . . . . . . Important Concepts Demonstrated . . . . . . . . . . . . . . . . . . . dlnode Class Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Creating Doubly Linked Lists Why a Handle Class for Doubly Linked Lists? . . . . . . . . . . Defining the dlnode Class . . . . . . . . . . . . . . . . . . . . . . . . . . . Specializing the dlnode Class . . . . . . . . . . . . . . . . . . . . . . . . Example — Class for Graphing Functions . . . . . . . . . . . . Display Fully Commented Example Code . . . . . . . . . . . . . . Class Definition Block . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Using the topo Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Behavior of the Handle Class . . . . . . . . . . . . . . . . . . . . . . . . 2-18 2-18 2-18 2-20 2-22 2-22 2-22 2-23 2-23 2-24 2-25 2-26 2-27 2-28 2-29 2-31 2-31 2-31 2-32 2-33 2-34 2-35 2-40 2-43 2-43 2-43 2-45 2-46 Class Definition—Syntax Reference Class Folders . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Options for Class Folders . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-2 3-2 3 iv Contents
More Information on Class Folders . . . . . . . . . . . . . . . . . . . Class Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Class Building Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . More In Depth Information . . . . . . . . . . . . . . . . . . . . . . . . . The Classdef Block . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Specifying Attributes and Superclasses . . . . . . . . . . . . . . . . Assigning Class Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Specifying Superclasses Specifying Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . What You Can Define . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . How to Initialize Property Values . . . . . . . . . . . . . . . . . . . . Defining Default Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . Assigning Property Values from Within the Constructor . . Initializing Properties to Unique Values . . . . . . . . . . . . . . . Property Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Property Access Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Referencing Object Properties Using Variables Specifying Methods and Functions . . . . . . . . . . . . . . . . . . The Methods Block . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Method Calling Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Methods In Separate Files . . . . . . . . . . . . . . . . . . . . . . . . . . Defining Private Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . More Detailed Information On Methods . . . . . . . . . . . . . . . Defining Class-Related Functions . . . . . . . . . . . . . . . . . . . . Events and Listeners . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Specifying Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Listening for Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Specifying Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Attribute Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Attribute Descriptions Specifying Attribute Values . . . . . . . . . . . . . . . . . . . . . . . . . Simpler Syntax for true/false Attributes . . . . . . . . . . . . . . . Calling Superclass Methods on Subclass Objects . . . . . Calling a Superclass Constructor . . . . . . . . . . . . . . . . . . . . . 3-4 3-5 3-5 3-6 3-6 3-7 3-7 3-7 3-8 3-8 3-8 3-9 3-9 3-10 3-10 3-11 3-11 3-13 3-13 3-14 3-14 3-16 3-16 3-16 3-18 3-18 3-18 3-20 3-20 3-20 3-21 3-21 3-23 3-23 v
Calling Superclass Methods . . . . . . . . . . . . . . . . . . . . . . . . . 3-24 A Class Code Listing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Example of Class Definition Syntax . . . . . . . . . . . . . . . . . . Understanding Code Analyzer Warnings . . . . . . . . . . . . . Syntax Warnings and Property Names . . . . . . . . . . . . . . . . Warnings Caused by Variable/Property Name Conflicts . . Exception to Variable/Property Name Rule . . . . . . . . . . . . . Functions Used with Objects . . . . . . . . . . . . . . . . . . . . . . . Functions to Query Class Members . . . . . . . . . . . . . . . . . . . Functions to Test Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . Using the Editor and Debugger with Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Referring to Class Files Modifying and Reloading Classes . . . . . . . . . . . . . . . . . . . Ensuring MATLAB Uses Your Changes . . . . . . . . . . . . . . . Compatibility with Previous Versions . . . . . . . . . . . . . . . New Class-Definition Syntax Introduced with MATLAB Software Version 7.6 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Changes to Class Constructors . . . . . . . . . . . . . . . . . . . . . . . New Features Introduced with Version 7.6 . . . . . . . . . . . . . Examples of Old and New . . . . . . . . . . . . . . . . . . . . . . . . . . . MATLAB and Other OO Languages . . . . . . . . . . . . . . . . . . Some Differences from C++ and Sun Java Code . . . . . . . . . Common Object-Oriented Techniques . . . . . . . . . . . . . . . . . 3-26 3-26 3-28 3-28 3-28 3-29 3-31 3-31 3-31 3-32 3-32 3-33 3-33 3-36 3-36 3-37 3-38 3-38 3-40 3-40 3-45 Defining and Organizing Classes Class Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . MATLAB User-Defined Classes . . . . . . . . . . . . . . . . . . . . . . 4-2 4-2 4 vi Contents
Defining Classes — Syntax . . . . . . . . . . . . . . . . . . . . . . . . . classdef Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Class Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Table of Class Attributes Specifying Attributes Using Expressions in Class Definitions . . . . . . . . . . . . . . Basic Knowledge . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where Can You Use Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . How MATLAB Evaluates Expressions Organizing Classes in Folders . . . . . . . . . . . . . . . . . . . . . . Options for Class Folders . . . . . . . . . . . . . . . . . . . . . . . . . . . @-Folders . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Path Folders . . . . . . . . . Access to Functions Defined in Private Folders Class Precedence and MATLAB Path . . . . . . . . . . . . . . . . . Specifying Class Precedence . . . . . . . . . . . . . . . . . . . . . . . . InferiorClasses Attribute . . . . . . . . . . . . . . . . . . . . . . . . . . . Scoping Classes with Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Package Folders Referencing Package Members Within Packages . . . . . . . . Referencing Package Members from Outside the Package . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Packages and the MATLAB Path . . . . . . . . . . . . . . . . . . . . . Importing Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Related Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Syntax for Importing Classes . . . . . . . . . . . . . . . . . . . . . . . . 4-4 4-4 4-5 4-5 4-6 4-7 4-7 4-7 4-9 4-13 4-13 4-13 4-14 4-14 4-14 4-16 4-16 4-18 4-18 4-19 4-19 4-21 4-23 4-23 4-23 Value or Handle Class — Which to Use 5 Comparing Handle and Value Classes . . . . . . . . . . . . . . . Why Select Handle or Value . . . . . . . . . . . . . . . . . . . . . . . . . 5-2 5-2 vii
Behavior of MATLAB Built-In Classes . . . . . . . . . . . . . . . . Behavior of User-Defined Classes . . . . . . . . . . . . . . . . . . . . Which Kind of Class to Use . . . . . . . . . . . . . . . . . . . . . . . . . Examples of Value and Handle Classes . . . . . . . . . . . . . . . . When to Use Handle Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . When to Use Value Classes The Handle Superclass . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Building on the Handle Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Handle Class Methods Relational Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Testing Handle Validity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Handle Class Delete Methods Finding Handle Objects and Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Finding Handle Objects Finding Handle Object Properties . . . . . . . . . . . . . . . . . . . . Implementing a Set/Get Interface for Properties . . . . . The Standard Set/Get Interface . . . . . . . . . . . . . . . . . . . . . . Property Get Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Property Set Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Subclassing hgsetget . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Controlling the Number of Instances . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Limiting Instances 5-2 5-4 5-9 5-9 5-9 5-10 5-11 5-11 5-12 5-12 5-13 5-14 5-18 5-18 5-18 5-20 5-20 5-20 5-21 5-21 5-24 5-24 Properties — Storing Class Data 6 How to Use Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . What Are Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Types of Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Defining Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Property Definition Block . . . . . . . . . . . . . . . . . . . . . . . . . . . Accessing Property Values . . . . . . . . . . . . . . . . . . . . . . . . . . 6-2 6-2 6-3 6-5 6-5 6-6 viii Contents
分享到:
收藏