learning to program with c# - 41 unit 4 examine the structure of the solutionexamine the structure...

13

Click here to load reader

Upload: sara-daniels

Post on 28-Mar-2015

213 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Learning to Program with C# - 41 Unit 4 Examine the structure of the solutionExamine the structure of the solution –classes/methods. Who calls what? How

Learning to Program with C# Learning to Program with C# - 4- 4

11

Unit 4Unit 4

• Examine the structure of the solutionExamine the structure of the solution– classes/methods. Who calls what? How does classes/methods. Who calls what? How does

it work? Show how ExecuteMission and it work? Show how ExecuteMission and Rocket/Arena fit into all of this.Rocket/Arena fit into all of this.

• Motivate the introduction of new Motivate the introduction of new behaviours without throwing away the oldbehaviours without throwing away the old

• Introduce inheritanceIntroduce inheritance• Show it happening in the Rocket example, Show it happening in the Rocket example,

and get some practiceand get some practice

Page 2: Learning to Program with C# - 41 Unit 4 Examine the structure of the solutionExamine the structure of the solution –classes/methods. Who calls what? How

Learning to Program with C# Learning to Program with C# - 4- 4

22

Examing the Rocket projectExaming the Rocket project• Three major classesThree major classes

– RocketControllerRocketController– RocketRocket– ArenaArena

• and one to get it all goingand one to get it all going– ExecuteMissionExecuteMission

• Creates an arena objectCreates an arena object• Creates a rocket object, passing it the arena to fly inCreates a rocket object, passing it the arena to fly in• Creates a rocket controller, passing it both the rocket and Creates a rocket controller, passing it both the rocket and

arenaarena• Instructs the rocket controller to start the mission – FlyInstructs the rocket controller to start the mission – Fly

– No objects are created by this class – not a factoryNo objects are created by this class – not a factory• one method – one method – MainMain – the starting point for the whole program – the starting point for the whole program

Page 3: Learning to Program with C# - 41 Unit 4 Examine the structure of the solutionExamine the structure of the solution –classes/methods. Who calls what? How

Learning to Program with C# Learning to Program with C# - 4- 4

33

A new kind of controller…A new kind of controller…

• Say we are happy with the flight Say we are happy with the flight executed by the controller…executed by the controller…– but, we now want to try a different flight,but, we now want to try a different flight,– and keep the old flight alsoand keep the old flight also

• Note the general case hereNote the general case here– A: We want to keep the old behaviourA: We want to keep the old behaviour– B: We want to keep all else the sameB: We want to keep all else the same

• just make an adjustment to the flight just make an adjustment to the flight instructionsinstructions

Page 4: Learning to Program with C# - 41 Unit 4 Examine the structure of the solutionExamine the structure of the solution –classes/methods. Who calls what? How

Learning to Program with C# Learning to Program with C# - 4- 4

44

Concept: Concept: InheritanceInheritance• We want to minimise the work involved in building We want to minimise the work involved in building

our new controllerour new controller– we want to we want to inheritinherit as much of the old controller's as much of the old controller's

structure as possiblestructure as possible– only creating anew that which we want changedonly creating anew that which we want changed

• InheritanceInheritance – a core concept in the object-oriented – a core concept in the object-oriented programming modelprogramming model– a key feature of large systems buildinga key feature of large systems building– if we have an item, and we want a new, related, item, if we have an item, and we want a new, related, item,

we have to build only that part which is different, we have to build only that part which is different, reusing unchanged partsreusing unchanged parts

– we build families of related classes/objectswe build families of related classes/objects

Page 5: Learning to Program with C# - 41 Unit 4 Examine the structure of the solutionExamine the structure of the solution –classes/methods. Who calls what? How

Learning to Program with C# Learning to Program with C# - 4- 4

55

Changing or adding Changing or adding behaviourbehaviour

• Here, we are proposing to change Here, we are proposing to change an existing behaviouran existing behaviour

• We could also use the inheritance We could also use the inheritance concept to add new behaviourconcept to add new behaviour– add methodsadd methods– for example, add an for example, add an AutoLandAutoLand

method to the rocket controllermethod to the rocket controller– we will examine this style laterwe will examine this style later

Page 6: Learning to Program with C# - 41 Unit 4 Examine the structure of the solutionExamine the structure of the solution –classes/methods. Who calls what? How

Learning to Program with C# Learning to Program with C# - 4- 4

66

Analogy – a car engine Analogy – a car engine factoryfactory

– these often have an engine management these often have an engine management chip in them, controlling how the engine chip in them, controlling how the engine runsruns

– in the factory, we can arrange to have one in the factory, we can arrange to have one kind of chip put into some cars and another kind of chip put into some cars and another kind put into other carskind put into other cars• we do not build a whole new engine plantwe do not build a whole new engine plant• we can pick alternate behaviourswe can pick alternate behaviours• in effect we have two factories, in that two kinds in effect we have two factories, in that two kinds

of engines are produced – but almost all the of engines are produced – but almost all the technology is shared and reusedtechnology is shared and reused

Page 7: Learning to Program with C# - 41 Unit 4 Examine the structure of the solutionExamine the structure of the solution –classes/methods. Who calls what? How

Learning to Program with C# Learning to Program with C# - 4- 4

77

Let's see this in action:Let's see this in action:

– Using the Object Browser, Using the Object Browser, click on the + next to click on the + next to RocketControllerRocketController• keep pressing the +s until you keep pressing the +s until you

get no moreget no more

– Do the same on + next to Do the same on + next to LoopingFlightLoopingFlight• RocketController inherits from RocketController inherits from

Object – this is a root class for Object – this is a root class for ALL classes, containing ALL classes, containing behaviour common to all behaviour common to all classesclasses

• MM

Page 8: Learning to Program with C# - 41 Unit 4 Examine the structure of the solutionExamine the structure of the solution –classes/methods. Who calls what? How

Learning to Program with C# Learning to Program with C# - 4- 4

88

The LoopingFlight ClassThe LoopingFlight Class

• Click on LoopingFlight in the Obj BrowserClick on LoopingFlight in the Obj Browser– In members window, there's only two membersIn members window, there's only two members

• LoopingFlight – a LoopingFlight – a constructorconstructor method method• Fly – the new version of Rocket Controller's FlyFly – the new version of Rocket Controller's Fly

– Compare this with RocketControllerCompare this with RocketController• there are additional members for the Rocket and the there are additional members for the Rocket and the

Arena that are being controlled, of which the Arena that are being controlled, of which the controller must keep trackcontroller must keep track

• there is no need to re-specify these in LoopingFlightthere is no need to re-specify these in LoopingFlight

Page 9: Learning to Program with C# - 41 Unit 4 Examine the structure of the solutionExamine the structure of the solution –classes/methods. Who calls what? How

Learning to Program with C# Learning to Program with C# - 4- 4

99

Running the LoopingFlight Running the LoopingFlight controller…controller…

– we are executing a different kind of missionwe are executing a different kind of mission– so, in the Object Browser, double click on so, in the Object Browser, double click on

ExecuteMission to open its code windowExecuteMission to open its code window• down near the bottom, you'll see the codedown near the bottom, you'll see the code

RocketController RC = new RocketController( rArena,…RocketController RC = new RocketController( rArena,…

• change change new RocketControllernew RocketController to to new LoopingFlightnew LoopingFlight• you are simply telling the system to use a different you are simply telling the system to use a different

kind of controller to fly the rocketkind of controller to fly the rocket• everything else stays the sameeverything else stays the same• now choose Start from the Debug menu as beforenow choose Start from the Debug menu as before

Page 10: Learning to Program with C# - 41 Unit 4 Examine the structure of the solutionExamine the structure of the solution –classes/methods. Who calls what? How

Learning to Program with C# Learning to Program with C# - 4- 4

1010

Create your own controllerCreate your own controller• Choose a flight style, ballistic missile perhaps…Choose a flight style, ballistic missile perhaps…

– takes off, flies left, then down nose-first into the groundtakes off, flies left, then down nose-first into the ground

• We need a new class in the project, inheriting from We need a new class in the project, inheriting from RocketControllerRocketController– choose menu item Project/Add Classchoose menu item Project/Add Class– select the Template select the Template Code FileCode File– type a class name – say type a class name – say BallisticFlight.csBallisticFlight.cs – then Enter – then Enter– open LoopingFlight and select and copy open LoopingFlight and select and copy allall the text in it the text in it– paste it into the empty BallisticFlight windowpaste it into the empty BallisticFlight window– change occurrences of LoopingFlight to BallisticFlightchange occurrences of LoopingFlight to BallisticFlight– adjust the comment in green to describe this flightadjust the comment in green to describe this flight– adjust ExecuteMission so that BallisticFlight is usedadjust ExecuteMission so that BallisticFlight is used– Debug/Start to see if it all works!!Debug/Start to see if it all works!!

Page 11: Learning to Program with C# - 41 Unit 4 Examine the structure of the solutionExamine the structure of the solution –classes/methods. Who calls what? How

Learning to Program with C# Learning to Program with C# - 4- 4

1111

Important terminologyImportant terminology• LoopingFlight is a LoopingFlight is a derived classderived class, , inheriting inheriting from from

RocketControllerRocketController• RocketController is a RocketController is a base classbase class of LoopingFlight of LoopingFlight• The Fly method of LoopingFlight The Fly method of LoopingFlight overridesoverrides the Fly method of the Fly method of

RocketControllerRocketController• In the code for RocketControllerIn the code for RocketController

– Fly is marked as a Fly is marked as a virtualvirtual method. This indicates that it can be method. This indicates that it can be overriden in the way we want in an inherited classoverriden in the way we want in an inherited class

• In the code for LoopingFlightIn the code for LoopingFlight– The inheritance relationship is made clear in the first line of the The inheritance relationship is made clear in the first line of the

class specificationclass specificationpublic class LoopingFlight : RocketController etc etcpublic class LoopingFlight : RocketController etc etc

– Fly is marked Fly is marked overrideoverride to show it is overriding the base Fly to show it is overriding the base Fly

Page 12: Learning to Program with C# - 41 Unit 4 Examine the structure of the solutionExamine the structure of the solution –classes/methods. Who calls what? How

Learning to Program with C# Learning to Program with C# - 4- 4

1212

Look around the code Look around the code files…files…

• Start to see the patterns that Start to see the patterns that repeat themselvesrepeat themselves– how is a class specifiedhow is a class specified– how is each method specifiedhow is each method specified

• Pick up the general structurePick up the general structure– we will address specifics as the we will address specifics as the

course progressescourse progresses– eventually, a text book will help you eventually, a text book will help you

outout

Page 13: Learning to Program with C# - 41 Unit 4 Examine the structure of the solutionExamine the structure of the solution –classes/methods. Who calls what? How

Learning to Program with C# Learning to Program with C# - 4- 4

1313

SummarySummary

• Introduced the concept of Introduced the concept of inheritanceinheritance– developing new classes that are related to developing new classes that are related to

existing classes at minimum costexisting classes at minimum cost

• Specifically, this timeSpecifically, this time– introduced base and derived classesintroduced base and derived classes– overriding existing behaviour with new overriding existing behaviour with new

behaviourbehaviour• adding new behaviour will be covered lateradding new behaviour will be covered later

– we used the we used the virtual methodvirtual method technique in the technique in the examplesexamples