MATLAB Programming
MATLAB Programming MATLAB Programming
9 Classes and Objects Classes and Objects: An Overview • “Features of Object-Oriented Programming” on page 9-3 • “MATLAB Data Class Hierarchy” on page 9-3 • “Creating Objects” on page 9-4 • “Invoking Methods on Objects” on page 9-4 • “Private Methods” on page 9-5 • “Helper Functions” on page 9-6 • “Debugging Class Methods” on page 9-6 • “Setting Up Class Directories” on page 9-6 • “Data Structure” on page 9-7 • “Tips for C++ and Java Programmers” on page 9-8 Youcanviewclassesasnewdatatypeshavingspecificbehaviorsdefined fortheclass. Forexample,apolynomial class might redefine the addition operator (+) so that it correctly performs the operation of addition on polynomials. Operations defined to work with objects of a particular class are known as methods of that class. You can also view classes as new items that you can treat as single entities. An example is an arrow object that MATLAB can display on graphs (perhaps composed of MATLAB line and patch objects) and that has properties like a Handle Graphics object. You can create an arrow simply by instantiating the arrow class. You can add classes to your MATLAB environment by specifying a MATLAB structure that provides data storage for the object and creating a class directory containing M-files that operate on the object. These M-files contain the methods for the class. The class directory can also include functions that define the way various MATLAB operators, including arithmetic operations, subscript referencing, and concatenation, apply to the objects. Redefining how abuilt-inoperatorworksforyourclassisknownasoverloading the operator. 9-2
Classes and Objects: An Overview Features of Object-Oriented Programming When using well-designed classes, object-oriented programming can significantly increase code reuse and make your programs easier to maintain and extend. Programming with classes and objects differs from ordinary structured programming in these important ways: • Function and operator overloading. You can create methods that override existing MATLAB functions. When you call a function with a user-defined object as an argument, MATLAB first checks to see if there is a method defined for the object’s class. If there is, MATLAB calls it, rather than the normal MATLAB function. • Encapsulation of data and methods. Object properties are not visible from the command line; you can access them only with class methods. This protects the object properties from operations that are not intended for the object’s class. • Inheritance. You can create class hierarchies of parent and child classes in which the child class inherits data fields and methods from the parent. A child class can inherit from one parent (single inheritance) ormanyparents (multiple inheritance). Inheritance can span one or more generations. Inheritance enables sharing common parent functions and enforcing common behavior amongst all child classes. • Aggregation. You can create classes using aggregation, inwhichanobject contains other objects. This is appropriate when an object type is part of another object type. For example, a savings account object might be a part of a financial portfolio object. MATLAB Data Class Hierarchy All MATLAB data types are designed to function as classes in object-oriented programming. The diagram below shows the fifteen fundamental data types (or classes) defined in MATLAB. You can add new data types to MATLAB by extending the class hierarchy. 9-3
- Page 612 and 613: 7 Working with Scientific Data Form
- Page 614 and 615: 7 Working with Scientific Data Form
- Page 616 and 617: 7 Working with Scientific Data Form
- Page 618 and 619: 7 Working with Scientific Data Form
- Page 620 and 621: 7 Working with Scientific Data Form
- Page 622 and 623: 7 Working with Scientific Data Form
- Page 624 and 625: 7 Working with Scientific Data Form
- Page 626 and 627: 7 Working with Scientific Data Form
- Page 628 and 629: 7 Working with Scientific Data Form
- Page 630 and 631: 7 Working with Scientific Data Form
- Page 632 and 633: 7 Working with Scientific Data Form
- Page 634 and 635: 7 Working with Scientific Data Form
- Page 636 and 637: 8 Error Handling Checking for Error
- Page 638 and 639: 8 Error Handling Handling and Recov
- Page 640 and 641: 8 Error Handling X = A * B catch di
- Page 642 and 643: 8 Error Handling matrixMultiply(A,
- Page 644 and 645: 8 Error Handling Message Identifier
- Page 646 and 647: 8 Error Handling error('msg_id', 'e
- Page 648 and 649: 8 Error Handling Warnings Like erro
- Page 650 and 651: 8 Error Handling Warning Control Th
- Page 652 and 653: 8 Error Handling warnings by issuin
- Page 654 and 655: 8 Error Handling You must type the
- Page 656 and 657: 8 Error Handling s(2) ans = identif
- Page 658 and 659: 8 Error Handling end function f2(x)
- Page 660 and 661: 8 Error Handling Debugging Errors a
- Page 664 and 665: 9 Classes and Objects The diagram s
- Page 666 and 667: 9 Classes and Objects functions do
- Page 668 and 669: 9 Classes and Objects visible only
- Page 670 and 671: 9 Classes and Objects Class Method
- Page 672 and 673: 9 Classes and Objects Examples of C
- Page 674 and 675: 9 Classes and Objects not support p
- Page 676 and 677: 9 Classes and Objects overloaded me
- Page 678 and 679: 9 Classes and Objects A(1,2).name(3
- Page 680 and 681: 9 Classes and Objects Object Indexi
- Page 682 and 683: 9 Classes and Objects Converter Met
- Page 684 and 685: 9 Classes and Objects Operation M-F
- Page 686 and 687: 9 Classes and Objects Example — A
- Page 688 and 689: 9 Classes and Objects to the .c fie
- Page 690 and 691: 9 Classes and Objects end if d >= 2
- Page 692 and 693: 9 Classes and Objects % SUBSREF swi
- Page 694 and 695: 9 Classes and Objects q = polynom(q
- Page 696 and 697: 9 Classes and Objects % POLYNOM/DIF
- Page 698 and 699: 9 Classes and Objects Building on O
- Page 700 and 701: 9 Classes and Objects Multiple Inhe
- Page 702 and 703: 9 Classes and Objects Inheritance M
- Page 704 and 705: 9 Classes and Objects a.currentValu
- Page 706 and 707: 9 Classes and Objects error('Asset
- Page 708 and 709: 9 Classes and Objects case 'date' a
- Page 710 and 711: 9 Classes and Objects Note that the
9 Classes and Objects<br />
Classes and Objects: An Overview<br />
• “Features of Object-Oriented <strong>Programming</strong>” on page 9-3<br />
• “<strong>MATLAB</strong> Data Class Hierarchy” on page 9-3<br />
• “Creating Objects” on page 9-4<br />
• “Invoking Methods on Objects” on page 9-4<br />
• “Private Methods” on page 9-5<br />
• “Helper Functions” on page 9-6<br />
• “Debugging Class Methods” on page 9-6<br />
• “Setting Up Class Directories” on page 9-6<br />
• “Data Structure” on page 9-7<br />
• “Tips for C++ and Java Programmers” on page 9-8<br />
Youcanviewclassesasnewdatatypeshavingspecificbehaviorsdefined<br />
fortheclass. Forexample,apolynomial class might redefine the addition<br />
operator (+) so that it correctly performs the operation of addition on<br />
polynomials. Operations defined to work with objects of a particular class<br />
are known as methods of that class.<br />
You can also view classes as new items that you can treat as single entities.<br />
An example is an arrow object that <strong>MATLAB</strong> can display on graphs (perhaps<br />
composed of <strong>MATLAB</strong> line and patch objects) and that has properties like a<br />
Handle Graphics object. You can create an arrow simply by instantiating<br />
the arrow class.<br />
You can add classes to your <strong>MATLAB</strong> environment by specifying a <strong>MATLAB</strong><br />
structure that provides data storage for the object and creating a class<br />
directory containing M-files that operate on the object. These M-files contain<br />
the methods for the class. The class directory can also include functions that<br />
define the way various <strong>MATLAB</strong> operators, including arithmetic operations,<br />
subscript referencing, and concatenation, apply to the objects. Redefining how<br />
abuilt-inoperatorworksforyourclassisknownasoverloading the operator.<br />
9-2