View
187
Download
1
Category
Tags:
Preview:
Citation preview
From last time• Hashes
• Creating hashes
• Putting information in hashes
• Iterating over hashes
• Classes
• Creating classes
• Putting variables and methods in classes
• Using methods in classes
Review: Creating an new object
How would I create a new Vehicle? Let's say I want to create a Prius with a max speed of 75.
Review: Methods in classes
Remember, to call a method that is a member of a class the '.' operator is used.
variable_name.method_name
Variable ScopesAll the class variables we have used so far have been marked
with an '@' symbol. This is because they are what is called instance variables.
!There are three types of variables we can use in classes:
global variables class variables
instance variables
Global VariablesA global variable is a variable that is available everywhere.
Global variables can be declared in two ways.
The first is already familiar to you: you just define the variable outside of any method or class, and it's global. If you want to
make a variable global from inside a method or class, just start it with a $, like so: $variable_name
Global VariablesGlobal Variables can be accessed normally. However, if the
global variable was defined within a class remember to use a '$' to access it.
Instance VariablesAn instance variable is only available to particular instances of
a class. !
You have already seen these. They are defined with the '@' symbol, and cannot be used outside of a class. You might
think of them as 'private' variables. Each class instance has its own unique instance variables.
Instance Variables
@name is an instance variable. It can only be used within one instance of a person class.
Class Variables
A class variable is available to all members of a certain class. !
A class variable is defined using '@@' in front of the variable name. Every single instance of the class has access to and
can modify the same variable.
Class VariablesWe can use class variables to do cool things such as keep
track of how many class instances we have.
Class Variables
Now the @@people_count variable will increase by one each time a new class instance is created. This variable value will
be stored and will continue to count up with each newly created class instance.
Inheritance
Inheritance is a really difficult topic so we're only going to do a little bit of it.
!Inheritance is the process by which one class takes on the
attributes and methods of another.
InheritanceInheritance is used to express a is-an relaltionship.
!For example, if we have a mammal class we could have
classes that inherit from the mammal class. Maybe we could have a cow class or a lemur class. In these cases, a cow is a
mammal and a lemur is a mammal.
InheritanceIn the example on the previous slide a SegFault is a
SuperBadError. In this case the SegFault class is inherited from the SuperBadError class. Therefore, even though the
instance created was a SegFault instance the variable err still has access to all methods contained within the SuperBadError
class.
InheritanceThis is the syntax for inheritance:
The derived class is the new class you're making and the base class is the class from which that new class inherits.
Inheritance Properties
When a class is derived from a base class the new class inherits all methods from the base class. The derived class
can use these methods just like they were written in the derived class itself.
Inheritance Properties
Though we didn't specify how a Cat should breathe, every cat will inherit that behaviour from the Mammal class since Cat was derived from Mammal. The Cat inherited the breathe
method and added its own speak method.
Inheritance Properties
What if we want our derived class to override one of the methods it inherits from the base class? All we have to do is
redefine the method in the derived class.
Inheritance PropertiesOn the previous slide the Penguin class inherited a fly method
from the Bird class. However, as a Penguin cannot fly the method was redefined in the Penguin class. When the fly
method is called on any Penguin instance the method defined in the Penguin class will be used instead of the method
defined in the Bird class.
Inheritance Properties
Just like the previous example, the Dragon class redefined the fight method that it inherited from the Creature class. Now if the fight method is called with a Dragon instance the fight
method defined in the Dragon class will be used.
Inheritance Properties Sometimes you'll be working with a derived class (or
subclass) and realize that you've overwritten a method or attribute defined in that class' base class (also called a parent
or superclass) that you actually need. !
We can get those methods back using the keyword "super."
Inheritance Properties
When you call super from inside a method, that tells Ruby to look in the superclass of the current class and find a method
with the same name as the one from which super is called. If it finds it, Ruby will use the superclass' version of the method.
One last note
In Ruby it is not possible to have a class with multiple base classes. For example, assume we have two classes: Person and Creature. If we define a Dragon class the Dragon cannot
inherit from both Person and Creature. We will get an error.
What we did today• Variable Scope
• Global Variables
• Instance Variables
• Class Variables
• Inheritance
• Inheriting Methods
• Overwriting Methods
• Super
Recommended