1 / 14

Variables and Inheritance Part 1

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 some information about the object. Examples: vehicle

havily
Télécharger la présentation

Variables and Inheritance Part 1

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Variables and InheritancePart 1 Alice

  2. 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.

  3. State • Each property stores some information about the object. • Examples: vehicle isShowing • The set of information about the object is called the state of the object.

  4. 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.

  5. Class-level Variables • New variables can be added to the properties list of an object. • 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.

  6. 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. • In this session, we will look at a simple example of adding a new class-level variable and inheritance.

  7. Example • A switch is potentially useful as a controller for an animation • switch handle up  animation running • switch handle down  animation not running • The problem is that the switch does not have a built-in property to track whether the switch handle is up or down.

  8. Creating a new variable • Create a new class-level variable named isOn. • The isOn variable is declared to be a Boolean type. • The value is initialized to true.

  9. On-off Controller Storyboard • The switch will act as an on-off controller. • When the user clicks on the switch, the handle will turn in the opposite direction to provide a visual cue. • A visual cue is helpful because variables are not visible. click: If isOn is true Do together set isOn to false turn the handle forward Else Do together set isOn to true turn the handle backward

  10. switch.click isOn property value is Boolean, so we don't need to write switch.isOn == true

  11. Save out • Save out the switch as a new class. • Rename the object (we used onOffSwitch) • Right-click on the object name and select save object…

  12. Import • Now you can add an instance of the newly created class to a new world.

  13. Event • An instance of the new class has • all the built-in properties and methods of the original Switch class, plus • isOn variable • click method • Still, you must use the Events editor to link a mouse-click event to the click method.

  14. Demo

More Related