7
Class 11 – Intro to Classes

Class 11 lecture notes

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Class 11 lecture notes

Class 11 – Intro to Classes

Page 2: Class 11 lecture notes

Agenda

Check-in Lateral Thinking Warm-Up Issues and Questions

Introducing Classes Built-in Classes Creating and Using Classes Wrap-up, Project Work, Q&A Preparing for Mid-Term

Page 3: Class 11 lecture notes

Introducing Classes

What is a Class? Objects and OOP/OOD An object represents a thing or entity Properties (state) Methods (behaviour) Rules & Procedures

Why use classes? Abstraction, instantiation, code re-use Encapsulation, interface, API Inheritance, polymorphism

Page 4: Class 11 lecture notes

The Ball Class

PROPERTIESSizeBase_ColorElasticityShapeDecal?

METHODSRollBounceCurve?FlySpin

Page 5: Class 11 lecture notes

Classes in Ruby

Built-in Classes http://ruby-doc.org/core/ Examples: Array, String, Matrix, Foo, Object Inheritance: see DateTime

Note: all attributes and behaviours are treated as methods in Ruby (def)

Custom Classes “class” keyword Capitalize class name “Get” and “set” methods Initialize with “new” method

Page 6: Class 11 lecture notes

A Class Act!

class Myball

def initialize end

def bounce (val) val.times do puts "Ball bounces" end end

def ballColor=(value) @color=value end

def ballColor @color end

end

myBall=Myball.new

myBall.bounce(3)

x=rand(3)

case x

when 0

myBall.ballColor="red"

when 1

myBall.ballColor="white"

when 2

myBall.ballColor="blue"

end

puts myBall.ballColor

Page 7: Class 11 lecture notes

Wrap-up

Summary Classes represent “things” Object Oriented Development models (or

abstracts) attributes and behaviours into classes

Objects are instances of a Class Next

Final Exam Wednesday Fishbone Diagram – Cause & Effect – Schedule? Assignments and project due at end of next

week (5pm Friday)