Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

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 does it work? Show how ExecuteMission and Rocket/Arena fit into all of this. Motivate the introduction of new behaviours without throwing away the oldMotivate the introduction of new behaviours without throwing away the old Introduce inheritanceIntroduce inheritance Show it happening in the Rocket example, and get some practiceShow it happening in the Rocket example, and get some practice

2 Learning to Program with C# - 42 Examing the Rocket project Three major classesThree major classes –RocketController –Rocket –Arena and one to get it all goingand one to get it all going –ExecuteMission 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 arenaCreates a rocket controller, passing it both the rocket and arena 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 factory one method – Main – the starting point for the whole programone method – Main – the starting point for the whole program

3 Learning to Program with C# - 43 A new kind of controller… Say we are happy with the flight executed by the controller…Say we are happy with the flight executed by the controller… –but, we now want to try a different flight, –and keep the old flight also Note the general case hereNote the general case here –A: We want to keep the old behaviour –B: We want to keep all else the same just make an adjustment to the flight instructionsjust make an adjustment to the flight instructions

4 Learning to Program with C# - 44 Concept: Inheritance We want to minimise the work involved in building our new controllerWe want to minimise the work involved in building our new controller –we want to inherit as much of the old controller's structure as possible –only creating anew that which we want changed Inheritance – a core concept in the object- oriented programming modelInheritance – a core concept in the object- oriented programming model –a key feature of large systems building –if we have an item, and we want a new, related, item, we have to build only that part which is different, reusing unchanged parts –we build families of related classes/objects

5 Learning to Program with C# - 45 Changing or adding behaviour Here, we are proposing to change an existing behaviourHere, we are proposing to change an existing behaviour We could also use the inheritance concept to add new behaviourWe could also use the inheritance concept to add new behaviour –add methods –for example, add an AutoLand method to the rocket controller –we will examine this style later

6 Learning to Program with C# - 46 Analogy – a car engine factory –these often have an engine management chip in them, controlling how the engine runs –in the factory, we can arrange to have one kind of chip put into some cars and another kind 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 of engines are produced – but almost all the technology is shared and reusedin effect we have two factories, in that two kinds of engines are produced – but almost all the technology is shared and reused

7 Learning to Program with C# - 47 Let's see this in action: –Using the Object Browser, click on the + next to RocketController keep pressing the +s until you get no morekeep pressing the +s until you get no more –Do the same on + next to LoopingFlight RocketController inherits from Object – this is a root class for ALL classes, containing behaviour common to all classesRocketController inherits from Object – this is a root class for ALL classes, containing behaviour common to all classes M

8 Learning to Program with C# - 48 The LoopingFlight Class Click on LoopingFlight in the Obj BrowserClick on LoopingFlight in the Obj Browser –In members window, there's only two members LoopingFlight – a constructor methodLoopingFlight – a constructor method Fly – the new version of Rocket Controller's FlyFly – the new version of Rocket Controller's Fly –Compare this with RocketController there are additional members for the Rocket and the Arena that are being controlled, of which the controller must keep trackthere are additional members for the Rocket and the Arena that are being controlled, of which the controller must keep track there is no need to re-specify these in LoopingFlightthere is no need to re-specify these in LoopingFlight

9 Learning to Program with C# - 49 Running the LoopingFlight controller… –we are executing a different kind of mission –so, in the Object Browser, double click on ExecuteMission 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,… change new RocketController to new LoopingFlightchange new RocketController to new LoopingFlight you are simply telling the system to use a different kind of controller to fly the rocketyou are simply telling the system to use a different kind 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

10 Learning to Program with C# - 410 Create 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 ground We need a new class in the project, inheriting from RocketControllerWe need a new class in the project, inheriting from RocketController –choose menu item Project/Add Class –select the Template Code File –type a class name – say BallisticFlight.cs – then Enter –open LoopingFlight and select and copy all the text in it –paste it into the empty BallisticFlight window –change occurrences of LoopingFlight to BallisticFlight –adjust the comment in green to describe this flight –adjust ExecuteMission so that BallisticFlight is used –Debug/Start to see if it all works!!

11 Learning to Program with C# - 411 Important terminology LoopingFlight is a derived class, inheriting from RocketControllerLoopingFlight is a derived class, inheriting from RocketController RocketController is a base class of LoopingFlightRocketController is a base class of LoopingFlight The Fly method of LoopingFlight overrides the Fly method of RocketControllerThe Fly method of LoopingFlight overrides the Fly method of RocketController In the code for RocketControllerIn the code for RocketController –Fly is marked as a virtual method. This indicates that it can be overriden 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 class specification public class LoopingFlight : RocketController etc etc –Fly is marked override to show it is overriding the base Fly

12 Learning to Program with C# - 412 Look around the code files… Start to see the patterns that repeat themselvesStart to see the patterns that repeat themselves –how is a class specified –how is each method specified Pick up the general structurePick up the general structure –we will address specifics as the course progresses –eventually, a text book will help you out

13 Learning to Program with C# - 413 Summary Introduced the concept of inheritanceIntroduced the concept of inheritance –developing new classes that are related to existing classes at minimum cost Specifically, this timeSpecifically, this time –introduced base and derived classes –overriding existing behaviour with new behaviour adding new behaviour will be covered lateradding new behaviour will be covered later –we used the virtual method technique in the examples


Download ppt "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."

Similar presentations


Ads by Google