acm init() day 6

52
ACM init() Day 6: December 4, 2014

Upload: ucla-association-of-computing-machinery

Post on 13-Jul-2015

184 views

Category:

Education


1 download

TRANSCRIPT

ACM init()Day 6: December 4, 2014

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: Defining a class

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: Creating an new object

Review: Methods in classes

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

variable_name.method_name

What would car2.how_safe print?

Answer

A Ford Pinto is likely to explode.

What would car3.speed print?

Answer

It's probably not legal to drive that fast.

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 Variables

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.

Instance Variables

So how do we access an instance variable?

Instance VariablesThe solution: a method.

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.

Class VariablesWe can access class variables the same way we accessed

instance variables.

This will print...

Current user: Kelly Manufacturer: Lenovo

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

Any questions on variables in classes?

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.

Inheritance

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

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 Properties

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

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.

Answer

Breathes fire!

Answer

Attacks with pointy teeth!

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 PropertiesHere is what the syntax looks like using 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.

Answer

This will print:

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

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.

DO NOT DO!!!

NONO

What we did today• Variable Scope

• Global Variables

• Instance Variables

• Class Variables

• Inheritance

• Inheriting Methods

• Overwriting Methods

• Super

Any questions?

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.