Top Banner
ACM init() Day 6: December 4, 2014
52
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: ACM init() Day 6

ACM init()Day 6: December 4, 2014

Page 2: ACM init() Day 6

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

Page 3: ACM init() Day 6

Review: Defining a class

Page 4: ACM init() Day 6

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.

Page 5: ACM init() Day 6

Review: Creating an new object

Page 6: ACM init() Day 6

Review: Methods in classes

Remember, to call a method that is a member of a class the '.' operator is used.

variable_name.method_name

Page 7: ACM init() Day 6

What would car2.how_safe print?

Page 8: ACM init() Day 6

Answer

A Ford Pinto is likely to explode.

Page 9: ACM init() Day 6

What would car3.speed print?

Page 10: ACM init() Day 6

Answer

It's probably not legal to drive that fast.

Page 11: ACM init() Day 6

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

Page 12: ACM init() Day 6

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

Page 13: ACM init() Day 6

Global Variables

Page 14: ACM init() Day 6

Global VariablesGlobal Variables can be accessed normally. However, if the

global variable was defined within a class remember to use a '$' to access it.

Page 15: ACM init() Day 6

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.

Page 16: ACM init() Day 6

Instance Variables

@name is an instance variable. It can only be used within one instance of a person class.

Page 17: ACM init() Day 6

Instance Variables

So how do we access an instance variable?

Page 18: ACM init() Day 6

Instance VariablesThe solution: a method.

Page 19: ACM init() Day 6

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.

Page 20: ACM init() Day 6

Class VariablesWe can use class variables to do cool things such as keep

track of how many class instances we have.

Page 21: ACM init() Day 6

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.

Page 22: ACM init() Day 6

Class VariablesWe can access class variables the same way we accessed

instance variables.

Page 23: ACM init() Day 6
Page 24: ACM init() Day 6

This will print...

Current user: Kelly Manufacturer: Lenovo

Files: {:hello=>"Hello, world!"}

Page 25: ACM init() Day 6

Any questions on variables in classes?

Page 26: ACM init() Day 6

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.

Page 27: ACM init() Day 6

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.

Page 28: ACM init() Day 6

Inheritance

Page 29: ACM init() Day 6

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.

Page 30: ACM init() Day 6

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.

Page 31: ACM init() Day 6

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.

Page 32: ACM init() Day 6

Inheritance Properties

Page 33: ACM init() Day 6

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.

Page 34: ACM init() Day 6

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.

Page 35: ACM init() Day 6

Inheritance Properties

Page 36: ACM init() Day 6

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.

Page 37: ACM init() Day 6

Inheritance Properties

Page 38: ACM init() Day 6

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.

Page 39: ACM init() Day 6
Page 40: ACM init() Day 6

Answer

Breathes fire!

Page 41: ACM init() Day 6
Page 42: ACM init() Day 6

Answer

Attacks with pointy teeth!

Page 43: ACM init() Day 6

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

Page 44: ACM init() Day 6

Inheritance PropertiesHere is what the syntax looks like using super:

Page 45: ACM init() Day 6

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.

Page 46: ACM init() Day 6
Page 47: ACM init() Day 6

Answer

This will print:

Instead of breathing fire... Attacks with pointy teeth!

Page 48: ACM init() Day 6

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.

Page 49: ACM init() Day 6

DO NOT DO!!!

NONO

Page 50: ACM init() Day 6

What we did today• Variable Scope

• Global Variables

• Instance Variables

• Class Variables

• Inheritance

• Inheriting Methods

• Overwriting Methods

• Super

Page 51: ACM init() Day 6

Any questions?

Page 52: ACM init() Day 6

Where to go from here

There are many resources online for coding help. Just google 'language-you-want-to-learn tutorial' and you will have multiple

options. Koding will work for most languages. !

Keep checking the Facebook page. There is a chance this will continue next quarter.