introduction to object-oriented matlab...object-oriented programming in matlab® helsinki 28/11/2012...

18
1 © 2016 The MathWorks, Inc. Introduction to Object-Oriented MATLAB Antti Löytynoja, Application Engineer

Upload: others

Post on 14-Sep-2020

14 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Introduction to Object-Oriented MATLAB...Object-Oriented Programming in MATLAB® Helsinki 28/11/2012 Author Antti Loytynoja Keywords Version 16.0 Created Date 4/22/2016 1:03:22 PM

1© 2016 The MathWorks, Inc.

Introduction to Object-Oriented MATLAB

Antti Löytynoja, Application Engineer

Page 2: Introduction to Object-Oriented MATLAB...Object-Oriented Programming in MATLAB® Helsinki 28/11/2012 Author Antti Loytynoja Keywords Version 16.0 Created Date 4/22/2016 1:03:22 PM

2

Demonstration: Procedural Programming

Simulate a bank account using a structure and

some functions

– Structures (data)

– Functions (actions)

Page 3: Introduction to Object-Oriented MATLAB...Object-Oriented Programming in MATLAB® Helsinki 28/11/2012 Author Antti Loytynoja Keywords Version 16.0 Created Date 4/22/2016 1:03:22 PM

4

Procedural Programming

Easy to learn

Minimal planning

No formal relationship between data and functions

Every detail is exposed

Page 4: Introduction to Object-Oriented MATLAB...Object-Oriented Programming in MATLAB® Helsinki 28/11/2012 Author Antti Loytynoja Keywords Version 16.0 Created Date 4/22/2016 1:03:22 PM

5

Why Object-Oriented Programming?

Build robust applications

– Build a relationship between data and actions

– Control data and actions

Design building blocks for developing applications

– Eases code reuse

– Enhance collaboration

Page 5: Introduction to Object-Oriented MATLAB...Object-Oriented Programming in MATLAB® Helsinki 28/11/2012 Author Antti Loytynoja Keywords Version 16.0 Created Date 4/22/2016 1:03:22 PM

6

Progression of Programming Techniques

Actions

Data

function

script

command line

value

variable

structure

classLevel of Abstraction / Sophistication

Application Complexity

Page 6: Introduction to Object-Oriented MATLAB...Object-Oriented Programming in MATLAB® Helsinki 28/11/2012 Author Antti Loytynoja Keywords Version 16.0 Created Date 4/22/2016 1:03:22 PM

7

Object-Oriented Terminology

Class

– Blueprint of an idea

– Properties (data)

– Methods (actions)

Object

– Specific example of a class

– Instance

– Any number of objects of a class can exist

An element – object

Definition – Class

Page 7: Introduction to Object-Oriented MATLAB...Object-Oriented Programming in MATLAB® Helsinki 28/11/2012 Author Antti Loytynoja Keywords Version 16.0 Created Date 4/22/2016 1:03:22 PM

14

Demonstration: Building a Simple Class

Describe a bank account using a class

– Properties (data)

– Methods (actions)

Use the object like a structure

Page 8: Introduction to Object-Oriented MATLAB...Object-Oriented Programming in MATLAB® Helsinki 28/11/2012 Author Antti Loytynoja Keywords Version 16.0 Created Date 4/22/2016 1:03:22 PM

15

Objects

Easy to create

Manage their own data

Interchangeable with a structure

– No other code changes required

– Properties behave similar to field names

– Can’t add fields arbitrarily

Page 9: Introduction to Object-Oriented MATLAB...Object-Oriented Programming in MATLAB® Helsinki 28/11/2012 Author Antti Loytynoja Keywords Version 16.0 Created Date 4/22/2016 1:03:22 PM

17

Objects with Methods

Have immediate access to

their own data (properties)

Allow you to overload

existing functions

Allow you to perform custom actions at

creation and deletion (constructor and destructor)

Page 10: Introduction to Object-Oriented MATLAB...Object-Oriented Programming in MATLAB® Helsinki 28/11/2012 Author Antti Loytynoja Keywords Version 16.0 Created Date 4/22/2016 1:03:22 PM

18

AccountAccount

Address

VIP statusDeactivate

Number

etc.

Credit

history

Balance

Name

Withdraw

Accumulate

Deposit

Encapsulation

Page 11: Introduction to Object-Oriented MATLAB...Object-Oriented Programming in MATLAB® Helsinki 28/11/2012 Author Antti Loytynoja Keywords Version 16.0 Created Date 4/22/2016 1:03:22 PM

19

Separates the interface

from the implementation

Simplifies object use

Becomes a building block

Withdraw

Accumulate

Deposit

Balance

Name

Encapsulation

Page 12: Introduction to Object-Oriented MATLAB...Object-Oriented Programming in MATLAB® Helsinki 28/11/2012 Author Antti Loytynoja Keywords Version 16.0 Created Date 4/22/2016 1:03:22 PM

20

Demonstration: Applying Attributes

Control access

Access = public

Access = protected

Restrict modification

Constant

Dependent

Page 13: Introduction to Object-Oriented MATLAB...Object-Oriented Programming in MATLAB® Helsinki 28/11/2012 Author Antti Loytynoja Keywords Version 16.0 Created Date 4/22/2016 1:03:22 PM

21

Using a Class as a Building Block

All accounts

Normal account

All private accounts

Credit account

Page 14: Introduction to Object-Oriented MATLAB...Object-Oriented Programming in MATLAB® Helsinki 28/11/2012 Author Antti Loytynoja Keywords Version 16.0 Created Date 4/22/2016 1:03:22 PM

22

Demonstration: Creating a Credit Account

Define a new class

CreditAccount

Inherit from the existing

class Account to reuse code

Add two additional properties,

overload withdraw -function

Use the credit account

Credit

Name

Balance

Page 15: Introduction to Object-Oriented MATLAB...Object-Oriented Programming in MATLAB® Helsinki 28/11/2012 Author Antti Loytynoja Keywords Version 16.0 Created Date 4/22/2016 1:03:22 PM

23

Inheritance

Subclass substitutes

for the superclass

Allows re-envisioning

and re-implementing

the superclass

Builds on proven code

Allows inheriting from the base MATLAB classes

Page 16: Introduction to Object-Oriented MATLAB...Object-Oriented Programming in MATLAB® Helsinki 28/11/2012 Author Antti Loytynoja Keywords Version 16.0 Created Date 4/22/2016 1:03:22 PM

24

Summary

O-O MATLAB is a sophisticated way of building MATLAB apps

– Follows the same principles as any other O-O language

Builds robust code (encapsulation)

Allows you to reuse code (inheritance)

Good when building larger apps and if many people contributing to/using

code

Page 17: Introduction to Object-Oriented MATLAB...Object-Oriented Programming in MATLAB® Helsinki 28/11/2012 Author Antti Loytynoja Keywords Version 16.0 Created Date 4/22/2016 1:03:22 PM

25

Additional material

http://se.mathworks.com/discovery/object-oriented-programming.html

Page 18: Introduction to Object-Oriented MATLAB...Object-Oriented Programming in MATLAB® Helsinki 28/11/2012 Author Antti Loytynoja Keywords Version 16.0 Created Date 4/22/2016 1:03:22 PM

47© 2016 The MathWorks, Inc.

Thank You!