1 / 37

Lecture 20

Lecture 20. Chapter 18 Object Oriented. Outline from Chapter 18. 18.1 Object-Oriented Programming 18.1.1 OO Background 18.1.2 Definitions 18.1.3 Concepts 18.1.4 MATLAB Observations 18.2 Categories of Classes 18.2.1 Modeling Physical Things 18.2.2 Modeling Collections

josiah-ward
Télécharger la présentation

Lecture 20

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 20 Chapter 18 Object Oriented

  2. Outline from Chapter 18 18.1 Object-Oriented Programming 18.1.1 OO Background 18.1.2 Definitions 18.1.3 Concepts 18.1.4 MATLAB Observations 18.2 Categories of Classes 18.2.1 Modeling Physical Things 18.2.2 Modeling Collections 18.2.3 Objects within Collections 18.3 MATLAB Implementation 18.3.1 MATLAB Classes 18.3.2 MATLAB Objects 18.3.3 MATLAB Attributes 18.3.4 MATLAB Methods 18.3.5 Encapsulation in MATLAB Classes 18.3.6 Inheritance inMATLAB Classes 18.3.7 MATLAB ParentClasses 18.3.8 MATLAB ChildClasses 18.3.9 Polymorphism inMATLAB 18.4 Example—Modeling Bank Accounts 18.4.1 The Base Class 18.4.2 Inheritance byExtension 18.4.3 Inheritance byRedefinition 18.5 Practical Example— Vehicle Modeling 18.5.1 A Vehicle Hierarchy 18.5.2 The Containment Relationship

  3. Object Oriented • What? • An object-oriented program may be viewed as a collection of cooperating objects, as opposed to a list of tasks (subroutines) to perform.* • Why? • language-imposed management of the interaction between large and small collections of programs and data • Helps organize our thoughts, clarifying • Definitions • Relationships, for example, generalization/refinement *modified from Wikipedia

  4. Object • Name • Simple components – have primitive types—called attributes • Methods: the functions that this object performs, often upon its attributes and upon any other objects that are its non-simple components

  5. Complex Components

  6. Inheritance, Component Part

  7. Relationships Among Objects

  8. Starting • An object can be invoked by a web-page. • An object can be “main”, and be invoked by the operating system. • In MATLAB, an object can be invoked by a script or in the command window.

  9. Invoking an Object in Code • Instructions (lines of code) are found inside methods. • To invoke another object, first construct it:ClassIWanttheObject = new ClassIWant(); • The above constructs a new instance of the type ClassIWant, and assigns the variable name theObject to refer to that object. • To use that object’s functions, call them as: • theObject.someAppropriateMethod(its Input ParameterList)

  10. What Classes? • People have already created lots of classes, and you can often obtain them to use. • You can create your own.

  11. Vehicle Class • In general, a vehicle would have a size, heading, location, speed, and route plan. It would have a method for moving along a street. Specific vehicles would have specific constraints—motorcycles might have a higher maximum speed, for example. Vehicle Size Heading Speed Route plan Maximum speed Primitive components: attributes Construct() Construct(copyThis) Move(some parameters) Methods are functions

  12. MATLAB Implementation • In MATLAB a class is a collection of functions (the methods of that class) stored as M-files in a specific subdirectory of the current workspace. • All of the public methods (those you want to be accessible from outside the class) must be stored in a directory named @NameOfTheClass. • Utilities to be used by one or more other methods but not from outside the class can be stored in a subdirectory named private.

  13. Required Methods Vehicle Size Heading Speed Route plan Maximum speed Construct() Construct(copyThis) display() char() setSize(newSize) getSize() setHeading(newHeading) getHeading() Etc.

  14. Example Constructor Listing 18.1 Constructor for the Fred class 1. function frd = Fred(data) 2. % @Fred\Fred class constructor. 3. % frd = Fred(data) creates a Fred containing data 4. % could also be a copy constructor: 5. % newF = Fred(oldF) copies the old Fred oldF 6. if nargin == 0 % did the user provide data? 7. frd.value = 0; % set the attribute 8. frd = class(frd,'Fred'); % cast as a Fred 9. elseifisa(data, 'Fred') % copy constructor? 10. frd = data; 11. else 12. frd.value = data; % data provided 13. frd = class(frd,'Fred'); 14. end

  15. Getting Around Call-by-Value for Object Oriented Behavior Listing 18.2 Fred’s add method 1. function add(frd, data) 2 % @Fred\add to add data to its value. 3. % add(frd, data) 4. frd.value = frd.value + data; 5. assignin('caller', inputname(1), frd); Line 5: Tunnels back to the caller’s workspace, finds the variable provided as the first parameter name, and copies our new object to that variable.

  16. MATLAB Implementation of Inheritance (IsA) • A local variable is first initialized as an instance of the parent class. The class cast method that creates the new child object includes this parent instance as a third parameter. The end result of this manipulation is an additional attribute in the child class that has the same name as the parent class and contains a reference to the parent object. When the child class needs access to the methods of the parent,8 it refers to these methods by way of this reference.

  17. MATLAB Inheritance Listing 18.3 Fred child constructor 1. function fc = FredChild(pd, cd) 2. % @FredChild class constructor. 3. % fc = FredChild (pd, cd) creates a FredChild 4. % whose parent value is pd, and local 5. % attribute is cd 6. % This could also be a copy constructor: 7. % fc = FredChild ( ofc ) copies the object ofc 8. if nargin == 0 9. super = Fred; 10. fc.cdata = 0; 11. fc = class(fc, 'FredChild', super); 12. elseifisa(pd, 'FredChild') 13. fc = pd; 14. else 15. super = Fred(pd); 16. fc.cdata = cd; 17. fc = class(fc, 'FredChild', super); 18. end

  18. Explanation of Inheritance Example • In Listing 18.3: • Lines 1–8: Show a typical header block for this constructor. • Line 9: When constructing a default child, we first construct a local • parent object named super with default contents. • Line 10: We set the child data items to default values. • Line 11: We include the parent object in the class cast to establish the • parent-child relationship. The actual result of including super is to • establish another field in the FredChild structure named Fred whose • content is a Fred object. • Lines 12 and 13: Show the copy constructor. • Line 15: The real constructor passes the pd parameter to create the • parent object. • Line 16: Sets the child data field. • Line 17: As with the default constructor, we establish the parentchild • link by passing the parent object to the class cast.

  19. Sending Back Results to a Structure • The addressing capability of assignin(...) is restricted to elementary variables; it cannot reach structure fields. So we have to extract the parent, add to it, and copy the result back to the parent. • 6. parent = fc.Fred; • 7. add(parent, value) • 8. fc.Fred = parent; • 9. assignin('caller', inputname(1), fc);

  20. BankAccount, with Specializations BankAccount balance Deposit Withdraw getBalance setBalance BankAccountWithOverdraftProtection SavingsBankAccount setBalance? Can I get an account like that?

  21. MATLAB OO Methods Listing 18.6 Parent’s display(...) method 1. function display(frd) 2. % @Fred\display method 3. disp(char(frd)) Listing 18.7 Parent’s char(...) method 1. function str = char(frd) 2. % @Fred\char method 3. str = sprintf('Fred with %d', frd.value );

  22. Constructor for Acct 1. function acct = BankAccount(data) 2. % @BankAccount\BankAccount class constructor. 3. % ba = BankAccount(amt) creates a bank account 4. % with balance amt 5. % could also be a copy constructor: 6. % ba = BankAccount( oba ) copies the account oba 7. if nargin == 0 8. acct.balance = 0; 9. acct = class(acct,'BankAccount'); 10. elseifisa(data, 'BankAccount') 11. acct = data; 12. else 13. acct.balance = data; 14. acct = class(acct,'BankAccount'); 15. end

  23. Explanation of Previous Code • Lines 1–6: Show the constructor header. • Lines 7–9: Show the default constructor. • Lines 12–15: Show the real constructor.

  24. Deposit Method • Listing 18.11 The BankAccount deposit method • 1. function deposit(acct, amount) • 2. % @BankAccount\deposit to the account • 3. setBalance(acct, acct.balance + amount); • 4. assignin('caller', inputname(1), acct);

  25. Explanation of the Previous Code • In Listing 18.11: • Lines 1 and 2: Show the method header. • Line 3: Invokes the setBalance(...) method (Listing 18.14) to add • the deposit to the current balance. • Line 4: Returns the new object with the updated balance. • Access to important data like the balance of an account should go through • access methods to allow for validation, writing audit trails, and so on to be • added in a central location.

  26. Withdraw Method • Listing 18.12 The BankAccount withdraw method • 1. function gets = withdraw(acct, amount) • 2. % @BankAccount\withdraw from the account • 3. gets = amount; • 4. if gets > acct.balance • 5. gets = acct.balance; • 6. end • 7. setBalance(acct, acct.balance - gets); • 8. assignin('caller', inputname(1), acct);

  27. Explanation of Withdraw Code • In Listing 18.12: • Lines 1 and 2: Show the method header. • Line 3: In a typical account, withdrawals are limited to the amount inthe account. Consequently, we must limit the amount returned to thecurrent balance. We initialize what the user gets to what he asked for. • Lines 4–6: If this is more money than the user has, give him only the current balance. • Line 7: Sets the new balance. • Line 8: Updates the object.

  28. Using Inheritance When Writing the Class that Inherits • Writing a SavingsAccount class involves only the constructor and two methods. • Calculate the interest on savings • The mandatory char() method • The SavingsAccount class will have all the characteristics of a BankAccount, plus the ability to calculate interest periodically.

  29. Savings Constructor Listing 18.19 SavingsAccount constructor 1. function acct = SavingsAccount(data) 2. % @SavingsAccount\SavingsAccount constructor. 3. % ba = SavingsAccount(amt) creates an account 4. % with balance amt 5. % could also be a copy constructor: 6. % ba = SavingsAccount( osa ) copies account osa 7. if nargin == 0 8. super = BankAccount; 9. acct.RATE = 5; 10. acct.MIN_BALANCE = 1000; 11. acct = class(acct, 'SavingsAccount', super); 12. elseifisa(data,'SavingsAccount') 13. acct = data; 14. else 15. super = BankAccount(data); 16. acct.RATE = 5; 17. acct.MIN_BALANCE = 1000; 18. acct = class(acct,'SavingsAccount', super); 19. end

  30. Explanation of Savings Constructor • Lines 1–6: Show the constructor header. • Line 7: Detects the need for the default constructor logic. • Line 8: Creates a default parent object. • Lines 9 and 10: Set the default local attributes • (constants in this example). • Line 11: Sets the class of the default object with • the parent object included. • Lines 12 and 13: Show the copy constructor. • Lines 15–19: Show the real constructor, which • differs only in passing the initial balance to the • parent constructor.

  31. The Specialization Listing 18.20 SavingsAccountcalcInterest method 1. function int = calcInterest(acct) 2. % @SavingsAccount\calcInterest 3. calculate the interest 4. if getBalance(acct) > acct.MIN_BALANCE 5. int = acct.RATE * getBalance(acct) / 100; 6. else 7. int = 0; 8. end

  32. Char method Listing 18.21 SavingsAccount char(...) method 1. function s = char(sa) 2. % @SavingsAccount\char for the SavingsAccount 3. % returns string representation of the account 4. s=sprintf( 'Savings %s', char(sa.BankAccount) );

  33. State Charts • Diagrammatic approach to specifying object behavior. • Initial concept was finite state machines. • Additional concept was AND and OR charts. • Harel State Charts

  34. Simple State Chart Example ligandNotBound binds Releases ligandBound

  35. AND states for Parallel State Multiple sites where Ac Bindings can occur. The state Of an acetylated nucleosome Can be described as the union Of the individual acetylation States of the several Possible acetylation sites

  36. AND States Diagram

  37. OR States Example

More Related