Variables and Inheritance Part 1 Alice Review Properties
Variables and Inheritance Part 1 Alice
Review: Properties A class defines properties for its own kind of object. When an object is created (instantiated), it receives its own set of properties.
State Each property stores information about the object. Examples: vehicle is. Showing The set of information about the object is called the state of the object.
State Change Instructions in our program code generally cause a change in a property value -- a state change. For example, a set instruction can be used to change the color of an object.
Class-level Variables New variables can be added to the properties list of an object. The new variable is class-level The value of the variable can be changed We say the variable is mutable. This extends the capability of an object to track state changes.
Example Game programs often make use of a timer – some mechanism to keep track of the time remaining in the game. We will construct a timer. First, add a 3 D text object to the world. Any string of digits could be used. We arbitrarily chose "0. 0"
Creating a new variable Select the timer object and then create a new variable named time. Left. The is. On variable is declared to be a Number type. The value is 0 (the game hasn't begun, as yet).
Start the timer At the start of the game, the timer's time. Left variable must be initialized to the amount of time allowed for playing the game. (This example uses seconds, but minutes would work just as well. ) Storyboard for a method to initialize the time. Left variable: initialize parameter: amount. Of. Time time. Left is set to a value specified by amount. Of. Time
Counting down In this example, the timer will count down to 0 (no seconds left on the clock). Storyboard for a count. Down method: count. Down Do in order While time. Left is greater than 0 Do in order update the 3 D text to show the remaining number of seconds decrease time. Left by 1 update the 3 D text to display 0 seconds Note: The last instruction updates the 3 D text display after the While loop ends.
class-level function To coordinate the game with the timer's countdown, write a function that returns the value of the time. Left variable.
Demo Ch 10 Lec 1 a. Timer Concepts illustrated in this example The value in a variable can be used in a conditional expression to control a loop. The value of time. Left is compared to 0. The value in a variable can be changed at runtime. The value of time. Left is decreased by 1 each time through the While loop. A number can be displayed as a text string by calling a built-in conversion function (as a string).
Save out the timer as a new class. Rename the object (we used timer. Down) Right-click on the object name and select save object…
Inheritance If the object (with its new variable) is saved out and given a new name, a new class is created. This is an example of inheritance. The new class inherits all the properties and methods (move, turn, roll, etc. ) of the original class. Also, the new class has the newly declared variable and an extended capability to track state changes.
Import Now you can add an instance of the newly created class to a new world.
Assignment Read Chapter 10 Section 1, up to page 257.
- Slides: 15