Presentation is loading. Please wait.

Presentation is loading. Please wait.

Inheritance in Matlab Classes E177 February 14, 2008 Copyright 2005-2008, Andy Packard. This work is licensed.

Similar presentations


Presentation on theme: "Inheritance in Matlab Classes E177 February 14, 2008 Copyright 2005-2008, Andy Packard. This work is licensed."— Presentation transcript:

1 Inheritance in Matlab Classes E177 February 14, 2008 http://jagger.me.berkeley.edu/~pack/e177 Copyright 2005-2008, Andy Packard. This work is licensed under the Creative Commons Attribution-ShareAlike License. To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/2.0/ or send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA. http://jagger.me.berkeley.edu/~pack/e177http://creativecommons.org/licenses/by-sa/2.0/

2 Classes Recall, classes give data an identity, and associate functions to the identity. A class can also inherit some parts of its identity and associated functions from another class. This is called inheritance, and allows for hierarchical layout of classes, and promotes reuse of methods. referred to as the child class referred to as the parent class

3 @vehicle class (front wheel steering) Simple constructor vehicle class function M = vehicle(xyloc,v,theta,omega,wh) M.Location = xyloc; % 2-by-1 M.Velocity = v; % 2-by-1 M.Orientation = theta; % 1-by-1 M.AngularRate = omega; % 1-by-1 M.RectDim = wh; % 2-by-1 M = class(M,’vehicle’); An ismoving method function B = ismoving(A) B = norm(A.Velocity)>0; An isrearwheelskidding method… some relation between Orientation and Velocity

4 @car class Simple constructor car class, inheriting vehicle function M = car(BP,SA,DT,R,xy,v,th,om,wh) Veh = vehicle(xy,v,th,om,wh); M.BreakPressure = BP; M.SteeringAngle = SA; M.DriveTorque = DT; M.WheelRadius = R; M = class(M,’car’,Veh); A = car(2.4,…); class(A) isa(A,’car’), isa(A,’vehicle’), ismoving(A) Make M an object of class car Inherit data from Veh, and all methods from class(Veh)

5 SomeCarMethod.m A method for objects of class car can reference and modify the inherited vehicle object function M = SomeCarMethod(A) % This function can access fields… A.BreakPressure % class=double A.SteeringAngle % class=double A.DriveTorque % class=double A.WheelRadius % class=double A.vehicle % class=vehicle Example: FrontWheelSlipAngle, relation between SteeringAngle, Orientation, Velocity

6 Scope for inherited classes At this point, we have Consider V1 = vehicle([2;3],[10;3],1.2,0.1,[1;4]); C1 = car(0,0.2,100,190,[1;2],[5;6],1,0,[2;4]); ismoving(V1) ismoving(C1) SomeCarMethod(V1) SomeCarMethod(C1) car.m SomeCarMethod.m @car vehicle.m ismoving.m isrealwheelskidding.m @vehicle Inside ismoving, only the fields of vehicle class can be accessed directly, even though input arg is a car object.

7 @bike class Simple constructor bike class, inheriting vehicle function M = bike(FCR,RCR,F,R,SA,RA,… xy,v,theta,wh) Veh = vehicle(xy,v,theta,wh); M.FrontChainRing = FCR; M.RearChainRing = RCR; M.FrontGear = F; M.RearGear = R; M.SteeringAngle = SA; M.RollAngle = RA; M = class(M,’bike’,Veh);

8 @wheelchair class Simple constructor wheelchair class, inheriting vehicle function M = wheelchair(RW,Lw,Rw,… xy,v,theta,wh) Veh = vehicle(xy,v,theta,wh); M.RearWheelRadius = RWR; M.LeftWheelRate = Lw; M.RightWheelRate = Rw; M = class(M,’wheelchair’,Veh); Front wheels are very small and light, so perhaps… function A = FrontWheelSlipAngle(W) A = 0;

9 Folder layout Each class has a constructor and methods. car.m FrontWheelSlipAngle.m... @car vehicle.m ismoving.m isrealwheelskidding @vehicle wheelchair.m FrontWheelSlipAngle.m... @wheelchair bike.m incrementFrontChainRing.m decrementFrontChainRing.m @bike

10 @ReferAssignGetSet Many ideas of get, set, subsref, subsasgn will to carry over to all classes we write. For example, –Reference of M.Value is the same as get(M,’Value’) –Multi-depth subsref has specific recursive meaning –Assignment of M.Value=B is the same as set(M,’Value’,B) –Multi-depth subsasgn has specific recursive meaning –In some cases, () reference and assignment should be handled by the built-in methods for Matlab arrays –set should work with or without output arguments –Special Matlab-like conventions for get and set should be followed Calls with 1-input argument Property names can be partially specified Multiple args in set(A,P1,V1,P2,V2) This is a good motivation to write a class that handles all of this functionality, and all other classes will simply inherit this.

11 @ReferAssignGetSet Questions/Options for a given class –Is property reference allowed, as in Object.Property –Is property assignment allowed, as in Object.Property = Value –Is () reference allowed, as in Object(Idx1,Idx2) if so, are builtin methods used, or does the class programmer supply the reference method? –Is () assignment allowed, as in Object(Idx1,Idx2) = Value if so, are builtin methods used, or does the class programmer supply the assignment method? –Is {} reference allowed, as in Object{Idx1,Idx2} if so, are builtin methods used, or does the class programmer supply the reference method? –Is {} assignment allowed, as in Object{Idx1,Idx2} = Value if so, are builtin methods used, or does the class programmer supply the assignment method? –Does set use assignin when called with no output arguments? set(A,Prop,Value) %uses inputname, overwrites A A = set(A,Prop,Value) % overwrites A, directly B = set(A,Prop,Value) % A unchanged


Download ppt "Inheritance in Matlab Classes E177 February 14, 2008 Copyright 2005-2008, Andy Packard. This work is licensed."

Similar presentations


Ads by Google