Top Banner
Resident Engagement – feedback from first stage and strategic level proposals 2 nd stage consultation events March 2012
16

OOP in VB. OOP Principles An object – in memory – has –Some data values members –Pieces of executable code methods Objects usually come and go during.

Apr 01, 2015

Download

Documents

Asa Thompson
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: OOP in VB. OOP Principles An object – in memory – has –Some data values members –Pieces of executable code methods Objects usually come and go during.

OOP in VB

Page 2: OOP in VB. OOP Principles An object – in memory – has –Some data values members –Pieces of executable code methods Objects usually come and go during.

OOP Principles

• An object – in memory – has– Some data values members– Pieces of executable code methods

• Objects usually come and go during execution – dynamic not static

• Objects belong to a class – the type of the object

• An object is an instantiation of the class

Page 3: OOP in VB. OOP Principles An object – in memory – has –Some data values members –Pieces of executable code methods Objects usually come and go during.

OOP Principles- Encapsulation

• Contrast with structured programming, where global data is processed by functions

• Encapsulation = enclosing the data in an object• Other code (in other classes) cannot change the

data in an object• No global data in pure OOP• High level of modularity• Data members should be private• Accessor methods of the class control access• Changes are (should be) validated

Page 4: OOP in VB. OOP Principles An object – in memory – has –Some data values members –Pieces of executable code methods Objects usually come and go during.

Typical classes (objects are nouns)

• Application-specific classes –– employee, invoice, contract, purchase order– dust particle in graphics for simulation

• System classes– UI – window, form, button, slider– System – thread, file, socket

Page 5: OOP in VB. OOP Principles An object – in memory – has –Some data values members –Pieces of executable code methods Objects usually come and go during.

Inheritance

• A class can be designed as a variation on another class (the base or ancestor class)

• The subclass inherits all the members and methods of the base class

• Supports code re-use – do not have to start from scratch

Page 6: OOP in VB. OOP Principles An object – in memory – has –Some data values members –Pieces of executable code methods Objects usually come and go during.

Polymorphism

• Sub-classes can have new or different data members of methods

• So can have variations on a theme

• eg a button is a sub-class of a window

Page 7: OOP in VB. OOP Principles An object – in memory – has –Some data values members –Pieces of executable code methods Objects usually come and go during.

Object lifetime

• An object is 'made' at run-time

• Using the keyword new

• Good OOP languages have constructors which make new objects

• and destructors to end objects and free up memory

Page 8: OOP in VB. OOP Principles An object – in memory – has –Some data values members –Pieces of executable code methods Objects usually come and go during.

References• In VB (and Java) classes are reference types• References are (hidden) pointers• No (exposed) pointers in VB or Java• In VB use Set eg-

Dim myObject as myClass ' say what type it is

Set myObject = new myClass ' actually make one

Dim object2 as myClass ' another reference

Set object2 = myObject ' to the same object

Page 9: OOP in VB. OOP Principles An object – in memory – has –Some data values members –Pieces of executable code methods Objects usually come and go during.

Garbage collection

• GC is when memory manager reclaims memory occupied by objects no longer needed – reused as free memory

• In C++ can explicitly destroy objects – memory reclaimed then

• In VB – system counts the number of references – when =0, GC happens

• Only important for large arrays of large objects

Page 10: OOP in VB. OOP Principles An object – in memory – has –Some data values members –Pieces of executable code methods Objects usually come and go during.

Classes in VB

• Class defined in a class module (name = name of class)

• Inside the class module you– declare private data members– methods as public or private sub or functions– Property Get or Let to access private data

members

Page 11: OOP in VB. OOP Principles An object – in memory – has –Some data values members –Pieces of executable code methods Objects usually come and go during.

Add Class module to project

• In Project.. Add class module

• Change name..

Page 12: OOP in VB. OOP Principles An object – in memory – has –Some data values members –Pieces of executable code methods Objects usually come and go during.

Example – particle object

• Idea – simulate physical system containing many particles

• Each particle needs data members – position and velocity

• And methods – initialise, move, and draw

• An array of these objects represents a bunch of particles

Page 13: OOP in VB. OOP Principles An object – in memory – has –Some data values members –Pieces of executable code methods Objects usually come and go during.

Look at the OOPParticle project

• Run it• Look through the code• Try changing the number of particles, the

viscous drag and so on.• Develop it –

– All the particles are green– Add data members for their red green and blue colour

components– Initialise them as random– Change the draw method to use the colour

Page 14: OOP in VB. OOP Principles An object – in memory – has –Some data values members –Pieces of executable code methods Objects usually come and go during.

Interfaces• In VB, an interface is a single way of 'talking to' different

classes• Interface written just like a class, but with empty methods• different classes can implement methods in different

ways• gives polymorphism

InterfaceMethod AMethod BMethod C

Class 1Method A (version 1)Method BMethod C

Class 2Method A (version 2)Method BMethod C

Class 3Method A (version 3)Method BMethod C

Calling code –Class1.methodAClass2.methodAClass3.methodA

Page 15: OOP in VB. OOP Principles An object – in memory – has –Some data values members –Pieces of executable code methods Objects usually come and go during.

Example interface - personnel

• Dealing with Employees

• All have name, rateOfPay properties

• All need a pay method

• Managerial employees are monthly paid

• Clerical employees are hourly paid

Page 16: OOP in VB. OOP Principles An object – in memory – has –Some data values members –Pieces of executable code methods Objects usually come and go during.

The Employee interface

Private nameStr As StringPrivate payRate As DoublePublic Property Let name(val As String)End PropertyPublic Property Get name() As StringEnd PropertyPublic Property Get rateOfPay() As DoubleEnd PropertyPublic Property Let rateOfPay(val As Double)End PropertyPublic Sub pay()End Sub

Page 17: OOP in VB. OOP Principles An object – in memory – has –Some data values members –Pieces of executable code methods Objects usually come and go during.

The Manager ClassImplements EmployeePrivate nameStr As StringPrivate payRate As DoublePublic Property Get Employee_name() As StringEmployee_name = nameStrEnd PropertyPublic Property Let Employee_name(val As String)nameStr = valEnd PropertyPublic Property Get Employee_rateOfPay() As DoubleEmployee_rateOfPay = payRateEnd PropertyPublic Property Let Employee_rateOfPay(val As Double)payRate = valEnd PropertyPublic Sub Employee_pay()MsgBox (nameStr & " gets paid " & payRate)End Sub

Page 18: OOP in VB. OOP Principles An object – in memory – has –Some data values members –Pieces of executable code methods Objects usually come and go during.

Testing the Manager class

Dim emp1 As Manager

Set emp1 = New Manager

emp1.Employee_name = "John (manager)"

emp1.Employee_rateOfPay = 2010

emp1.Employee_pay

Page 19: OOP in VB. OOP Principles An object – in memory – has –Some data values members –Pieces of executable code methods Objects usually come and go during.

The Clerical Class (interface example)Implements EmployeePrivate nameStr As StringPrivate payRate As Double' extra data memberPrivate hours As Integer'extra propertiesPublic Property Let hoursWorked(val As Integer)hours = valEnd PropertyPublic Property Get hoursWorked() As IntegerhoursWorked = hoursEnd Property..rest same as Manager class except..Public Sub Employee_pay()MsgBox (nameStr & " gets paid " & payRate * hours)End Sub

Page 20: OOP in VB. OOP Principles An object – in memory – has –Some data values members –Pieces of executable code methods Objects usually come and go during.

Using the Clerical Class

Dim emp2 As ClericalSet emp2 = New Clericalemp2.Employee_name = "Luke (clerical)"emp2.Employee_rateOfPay = 8.5emp2.hoursWorked = 100emp2.Employee_pay

Page 21: OOP in VB. OOP Principles An object – in memory – has –Some data values members –Pieces of executable code methods Objects usually come and go during.

Interfaces are not class hierarchies

•There is no inheritance in VB6•There are no derived classes in VB•In the above, Clerical is not a sub-class of Employee•Members must be re-coded