class-level methods and inheritance part 1
DESCRIPTION
Class-level Methods and Inheritance Part 1. Alice. Animated Actions. Some actions are more naturally associated with a specific class of objects rather than with the overall world. Examples A person walking A wheel rolling A fish swimming. Class-level Methods. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Class-level Methods and Inheritance Part 1](https://reader031.vdocuments.net/reader031/viewer/2022020721/56812b26550346895d8f2bab/html5/thumbnails/1.jpg)
Class-level Methods and Inheritance
Part 1Alice
![Page 2: Class-level Methods and Inheritance Part 1](https://reader031.vdocuments.net/reader031/viewer/2022020721/56812b26550346895d8f2bab/html5/thumbnails/2.jpg)
Animated Actions Some actions are more naturally associated with a specific class of objects rather than with the overall world.
ExamplesA person walking
A wheel rolling
A fish swimming
![Page 3: Class-level Methods and Inheritance Part 1](https://reader031.vdocuments.net/reader031/viewer/2022020721/56812b26550346895d8f2bab/html5/thumbnails/3.jpg)
Class-level Methods
We can write our own methods to add abilities/functions to a specific class of objects
This will result in a class-level rather than a world-level method
The next few slides will demonstrate how to build class-level methods
![Page 4: Class-level Methods and Inheritance Part 1](https://reader031.vdocuments.net/reader031/viewer/2022020721/56812b26550346895d8f2bab/html5/thumbnails/4.jpg)
An example (building technique)
How can we create a skate method for ice skater objects?
We need to:
(1) tell Alice to associate the new method (the one we are about to write) with an ice skater, and
(2) write a new method to animate the ice skater skating.
![Page 5: Class-level Methods and Inheritance Part 1](https://reader031.vdocuments.net/reader031/viewer/2022020721/56812b26550346895d8f2bab/html5/thumbnails/5.jpg)
Demo: The solution
First, to associate the animation with the ice skater
• select the iceSkater tile in the Object Tree
•select the methods tab in the details area
•click on the "create new method" button
![Page 6: Class-level Methods and Inheritance Part 1](https://reader031.vdocuments.net/reader031/viewer/2022020721/56812b26550346895d8f2bab/html5/thumbnails/6.jpg)
Storyboard for skate
The slide actions each require several motion instructions, so we will break these two actions down into smaller pieces… this technique is called stepwise refinement
Skate:Do together move skater forward 2 meters Do in order slide on left leg slide on right leg
![Page 7: Class-level Methods and Inheritance Part 1](https://reader031.vdocuments.net/reader031/viewer/2022020721/56812b26550346895d8f2bab/html5/thumbnails/7.jpg)
Refined storyboard for skate
Refinement of slideLeftDo in order Lift right leg and turn upper body forward Lower right leg and return body upright Skate:
Do together 1) move forward 2 meters 2) Do in order slideLeft slideRight
Refinement of slideRightDo in order Lift left leg and turn upper body forward Lower left leg and return body upright
![Page 8: Class-level Methods and Inheritance Part 1](https://reader031.vdocuments.net/reader031/viewer/2022020721/56812b26550346895d8f2bab/html5/thumbnails/8.jpg)
Writing the code
The next step is to translate the design into program code.For the slideLeft method, a possible translation is:
Design step InstructionLift the right leg turn the right thigh forwardTurn upper body forward turn the abs forward Lower the right leg turn the right thigh backward
Return the body upright turn the abs backward
![Page 9: Class-level Methods and Inheritance Part 1](https://reader031.vdocuments.net/reader031/viewer/2022020721/56812b26550346895d8f2bab/html5/thumbnails/9.jpg)
Demo
Demonstration of slideLeft
slideRight
skate
![Page 10: Class-level Methods and Inheritance Part 1](https://reader031.vdocuments.net/reader031/viewer/2022020721/56812b26550346895d8f2bab/html5/thumbnails/10.jpg)
Correspondence of design to code
Skate:Do together move skater forward 2 meters Do in order slide on left leg slide on right leg
![Page 11: Class-level Methods and Inheritance Part 1](https://reader031.vdocuments.net/reader031/viewer/2022020721/56812b26550346895d8f2bab/html5/thumbnails/11.jpg)
Question
Writing the methods to make an ice skater perform a skating motion is an intricate task.
Most likely, you would like to reuse these new methods in other worlds.
How can you make the skate method available for an ice skater in a different world?
![Page 12: Class-level Methods and Inheritance Part 1](https://reader031.vdocuments.net/reader031/viewer/2022020721/56812b26550346895d8f2bab/html5/thumbnails/12.jpg)
Answer: Save out as a new class
1) Rename iceSkater as cleverSkater.
2) Save out as a new class. Alice saves the new class as CleverSkater.a2c
![Page 13: Class-level Methods and Inheritance Part 1](https://reader031.vdocuments.net/reader031/viewer/2022020721/56812b26550346895d8f2bab/html5/thumbnails/13.jpg)
Inheritance
The CleverSkater class inherits all the properties and methods from the original IceSkater class, and also
has the newly defined methods (skate, slideLeft, slideRight)
In other programming languages, the concept of creating a new class based on a previously defined class is called inheritance.
![Page 14: Class-level Methods and Inheritance Part 1](https://reader031.vdocuments.net/reader031/viewer/2022020721/56812b26550346895d8f2bab/html5/thumbnails/14.jpg)
Using CleverSkater
An instance of the CleverSkater class can be added to a new world – use File|Import.
![Page 15: Class-level Methods and Inheritance Part 1](https://reader031.vdocuments.net/reader031/viewer/2022020721/56812b26550346895d8f2bab/html5/thumbnails/15.jpg)
Benefits of Inheritance
Inheritance supports the reuse of program code -- allows the programmer to write code once and use it again later in different programs.
sharing code with others in a team project
![Page 16: Class-level Methods and Inheritance Part 1](https://reader031.vdocuments.net/reader031/viewer/2022020721/56812b26550346895d8f2bab/html5/thumbnails/16.jpg)
Assignment
Read Chapter 4-3 Class-level methods and Inheritance
Read Tips & Techniques 4 Visible and Invisible Objects