
MATLAB® Object-Oriented Programming R2020a How to Contact MathWorks Latest news: www.mathworks.com Sales and services: www.mathworks.com/sales_and_services User community: www.mathworks.com/matlabcentral Technical support: www.mathworks.com/support/contact_us Phone: 508-647-7000 The MathWorks, Inc. 1 Apple Hill Drive Natick, MA 01760-2098 Object-Oriented Programming © COPYRIGHT 1984–2020 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 MathWorks products are protected by one or more U.S. patents. Please see www.mathworks.com/patents for more information. Revision History March 2008 Online only New for MATLAB 7.6 (Release 2008a) October 2008 Online only Revised for MATLAB 7.7 (Release 2008b) March 2009 Online only Revised for MATLAB 7.8 (Release 2009a) September 2009 Online only Revised for MATLAB 7.9 (Release 2009b) March 2010 Online only Revised for MATLAB 7.10 (Release 2010a) September 2010 Online only Revised for Version 7.11 (Release 2010b) April 2011 Online only Revised for Version 7.12 (Release 2011a) September 2011 Online only Revised for Version 7.13 (Release 2011b) March 2012 Online only Revised for Version 7.14 (Release 2012a) September 2012 Online only Revised for Version 8.0 (Release 2012b) March 2013 Online only Revised for Version 8.1 (Release 2013a) September 2013 Online only Revised for Version 8.2 (Release 2013b) March 2014 Online only Revised for Version 8.3 (Release 2014a) October 2014 Online only Revised for Version 8.4 (Release 2014b) March 2015 Online only Revised for Version 8.5 (Release 2015a) September 2015 Online only Revised for Version 8.6 (Release 2015b) March 2016 Online only Revised for Version 9.0 (Release 2016a) September 2016 Online only Revised for Version 9.1 (Release 2016b) March 2017 Online only Revised for Version 9.2 (Release 2017a) September 2017 Online only Revised for Version 9.3 (Release 2017b) March 2018 Online only Revised for MATLAB 9.4 (Release 2018a) September 2018 Online only Revised for Version 9.5 (Release 2018b) March 2019 Online only Revised for MATLAB 9.6 (Release 2019a) September 2019 Online only Revised for MATLAB 9.7 (Release 2019b) March 2020 Online only Revised for MATLAB 9.8 (Release 2020a) Contents Using Object-Oriented Design in MATLAB 1 Why Use Object-Oriented Design ................................. 1-2 Approaches to Writing MATLAB Programs ........................ 1-2 When Should You Create Object-Oriented Programs ................. 1-2 Handle Object Behavior ........................................ 1-7 What Is a Handle? .......................................... 1-7 Copies of Handles ........................................... 1-7 Handle Objects Modified in Functions ............................ 1-8 Determine If an Object Is a Handle .............................. 1-9 Deleted Handle Objects ...................................... 1-9 Basic Example 2 Create a Simple Class .......................................... 2-2 Design Class ............................................... 2-2 Create Object .............................................. 2-3 Access Properties ........................................... 2-3 Call Methods .............................................. 2-3 Add Constructor ............................................ 2-4 Vectorize Methods .......................................... 2-4 Overload Functions ......................................... 2-5 BasicClass Code Listing ...................................... 2-6 MATLAB Classes Overview 3 Role of Classes in MATLAB ...................................... 3-2 Classes ................................................... 3-2 Some Basic Relationships ..................................... 3-3 Developing Classes — Typical Workflow ............................ 3-6 Formulating a Class ......................................... 3-6 Specifying Class Components .................................. 3-7 BankAccount Class Implementation ............................. 3-7 Formulating the AccountManager Class ......................... 3-10 Implementing the AccountManager Class ........................ 3-11 AccountManager Class Synopsis ............................... 3-11 v Using BankAccount Objects .................................. 3-12 Representing Structured Data with Classes ....................... 3-14 Objects as Data Structures ................................... 3-14 Structure of the Data ....................................... 3-14 The TensileData Class ....................................... 3-15 Create an Instance and Assign Data ............................ 3-15 Restrict Properties to Specific Values ........................... 3-16 Simplifying the Interface with a Constructor ...................... 3-16 Calculate Data on Demand ................................... 3-17 Displaying TensileData Objects ................................ 3-18 Method to Plot Stress vs. Strain ............................... 3-18 TensileData Class Synopsis ................................... 3-19 Implementing Linked Lists with Classes .......................... 3-23 Class Definition Code ....................................... 3-23 dlnode Class Design ........................................ 3-23 Create Doubly Linked List ................................... 3-24 Why a Handle Class for Linked Lists? ........................... 3-25 dlnode Class Synopsis ...................................... 3-25 Specialize the dlnode Class ................................... 3-34 Static Data 4 Static Data ................................................... 4-2 What Is Static Data .......................................... 4-2 Static Variable ............................................. 4-2 Static Data Object .......................................... 4-3 Constant Data .............................................. 4-4 Class Definition—Syntax Reference 5 Class Files and Folders ......................................... 5-2 Class Definition Files ........................................ 5-2 Options for Class Folders ..................................... 5-2 Options for Class Files ....................................... 5-2 Group Classes with Package Folders ............................. 5-3 Class Components ............................................. 5-4 Class Building Blocks ........................................ 5-4 Class Definition Block ........................................ 5-4 Properties Block ............................................ 5-5 Methods Block ............................................. 5-5 Events Block ............................................... 5-5 A Complete Class ........................................... 5-6 Enumeration Classes ........................................ 5-6 Related Information ......................................... 5-7 vi Contents Classdef Block ................................................ 5-8 How to Specify Attributes and Superclasses ....................... 5-8 Class Attribute Syntax ....................................... 5-8 Superclass Syntax .......................................... 5-8 Local Functions in Class File .................................. 5-9 Class Properties .............................................. 5-10 The Properties Block ....................................... 5-10 Access to Property Values .................................... 5-11 Methods and Functions ........................................ 5-13 The Methods Block ......................................... 5-13 Method Calling Syntax ...................................... 5-13 Private Methods ........................................... 5-14 More Detailed Information on Methods .......................... 5-14 Class-Related Functions ..................................... 5-14 How to Overload Functions and Operators ....................... 5-15 Rules for Defining Methods in Separate Files ..................... 5-15 Events and Listeners .......................................... 5-17 Define and Trigger Events ..................................
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages684 Page
-
File Size-