• No results found

Classes and Objects. Introduction

N/A
N/A
Protected

Academic year: 2021

Share "Classes and Objects. Introduction"

Copied!
40
0
0

Loading.... (view fulltext now)

Full text

(1)

Introduction

Copyright © Software Carpentry 2010

This work is licensed under the Creative Commons Attribution License See http://software-carpentry.org/license.html for more information.

(2)

Classes and Objects Introduction

(3)

Classes and Objects Introduction

Computer science is the study of algorithms Computer programming is about creating and composing abstractions

(4)

Classes and Objects Introduction

Computer science is the study of algorithms Computer programming is about creating and composing abstractions

(5)

Classes and Objects Introduction

Computer science is the study of algorithms Computer programming is about creating and composing abstractions

hide the details make one thing act like another

(6)

Classes and Objects Introduction

Computer science is the study of algorithms Computer programming is about creating and composing abstractions

hide the details make one thing act like another Functions turn many steps into one (logical) step

(7)

Classes and Objects Introduction

Computer science is the study of algorithms Computer programming is about creating and composing abstractions

hide the details make one thing act like another Functions turn many steps into one (logical) step

(8)

Classes and Objects Introduction

Computer science is the study of algorithms Computer programming is about creating and composing abstractions

hide the details make one thing act like another Functions turn many steps into one (logical) step

Libraries group functions to make them manageable Classes and objects combine functions and data

(9)

Classes and Objects Introduction

Computer science is the study of algorithms Computer programming is about creating and composing abstractions

hide the details make one thing act like another Functions turn many steps into one (logical) step

Libraries group functions to make them manageable Classes and objects combine functions and data

(10)

Classes and Objects Introduction

(11)

Classes and Objects Introduction

Simple simulation of aquarium containing plants

(12)

Classes and Objects Introduction

Simple simulation of aquarium containing plants snails

(13)

Classes and Objects Introduction

Simple simulation of aquarium containing

(14)

Classes and Objects Introduction

Simple simulation of aquarium containing

plants snails fish

don't move photosynthesize

(15)

Classes and Objects Introduction

Simple simulation of aquarium containing

plants snails fish

don't move crawl in 2D photosynthesize scavenge

(16)

Classes and Objects Introduction

Simple simulation of aquarium containing

plants snails fish

don't move crawl in 2D swim in 3D photosynthesize scavenge hunt

(17)

Classes and Objects Introduction

Simple simulation of aquarium containing

plants snails fish

don't move crawl in 2D swim in 3D photosynthesize scavenge hunt

(18)

Classes and Objects Introduction

Simple simulation of aquarium containing

plants snails fish

don't move crawl in 2D swim in 3D photosynthesize scavenge hunt

Algorithm is simple for t in

range(timesteps): move(world, everything) eat(world, everything) show(world,

(19)

Classes and Objects Introduction

Simple simulation of aquarium containing

plants snails fish

don't move crawl in 2D swim in 3D photosynthesize scavenge hunt

Algorithm is simple

Program is more complicated

for t in range(timesteps): move(world, everything) eat(world, everything) show(world,

(20)

Classes and Objects Introduction

def move(world, everything): for thing in everything: if thing[0] == 'plant':

pass # plants don't move

elif thing[0] == 'snail': move_snail(snail)

elif thing[0] == 'fish': move_fish(fish)

(21)

Classes and Objects Introduction

def move(world, everything): for thing in everything: if thing[0] == 'plant':

pass # plants don't move

elif thing[0] == 'snail': move_snail(snail)

elif thing[0] == 'fish': move_fish(fish)

(22)

Classes and Objects Introduction

def eat(world, everything): for thing in everything: if thing[0] == 'plant':

photosynthesize(world, plant) elif thing[0] == 'snail':

scavenge(world, snail) elif thing[0] == 'fish':

prey = hunt(world, everything, thing)

if prey != None:

devour(world, everything, thing, prey)

(23)

Classes and Objects Introduction

def eat(world, everything): for thing in everything: if thing[0] == 'plant':

photosynthesize(world, plant) elif thing[0] == 'snail':

scavenge(world, snail) elif thing[0] == 'fish':

prey = hunt(world, everything, thing)

if prey != None:

devour(world, everything, thing, prey)

(24)

Classes and Objects Introduction

def show(world, everything): show_world(world)

for thing in everything: if thing[0] == 'plant': show_plant(plant)

elif thing[0] == 'snail': show_snail(snail)

elif thing[0] == 'fish': show_fish(fish)

(25)

Classes and Objects Introduction

def show(world, everything): show_world(world)

for thing in everything: if thing[0] == 'plant': show_plant(plant)

elif thing[0] == 'snail': show_snail(snail)

elif thing[0] == 'fish': show_fish(fish)

(26)

Classes and Objects Introduction

Pessimist: code that's repeated in two or more places will eventually be wrong in at least one

(27)

Classes and Objects Introduction

Pessimist: code that's repeated in two or more places will eventually be wrong in at least one

(28)

Classes and Objects Introduction

Pessimist: code that's repeated in two or more places will eventually be wrong in at least one

To add starfish, we have to modify three functions remember to

(29)

Classes and Objects Introduction

Pessimist: code that's repeated in two or more places will eventually be wrong in at least one

To add starfish, we have to modify three functions remember to

(30)

Classes and Objects Introduction

Pessimist: code that's repeated in two or more places will eventually be wrong in at least one

To add starfish, we have to modify three functions remember to

What about fish that eat plants? Or scavenge? Optimist: every pattern in a program is an

(31)

Classes and Objects Introduction

(32)

Classes and Objects Introduction

for thing in everything: thing.move()

prey = thing.eat(everything) if prey:

thing.devour(prey)

everything.remove(prey)

(33)

Classes and Objects Introduction

Wouldn't this be simpler?

Easier to understand (after some practice)

for thing in everything: thing.move()

prey = thing.eat(everything) if prey:

thing.devour(prey)

(34)

Classes and Objects Introduction

Wouldn't this be simpler?

Easier to understand (after some practice) Much easier to add new kinds of things

for thing in everything: thing.move()

prey = thing.eat(everything) if prey:

thing.devour(prey)

(35)

Classes and Objects Introduction

(36)

Classes and Objects Introduction

Nothing is free

(37)

Classes and Objects Introduction

Nothing is free

Simple programs become slightly more complex And too much abstraction creates as big a mental burden as too little

(38)

Classes and Objects Introduction

Nothing is free

Simple programs become slightly more complex And too much abstraction creates as big a mental burden as too little

Degree of Abstraction M e n tal E ff or t R e q ui re d

Putting steps together to get big picture

(39)

Classes and Objects Introduction

Nothing is free

Simple programs become slightly more complex And too much abstraction creates as big a mental burden as too little

Degree of Abstraction M e n tal E ff or t R e q ui re d

Putting steps together to get big picture

Tracing steps to figure out what actually happens

(40)

January 2011

Copyright © Software Carpentry 2010

This work is licensed under the Creative Commons Attribution License See http://software-carpentry.org/license.html for more information.

created by

References

Related documents

The compact manager is in charge of compact construction, commit of locally committed transactions (synchro- nization, permanents updates) Connected, disconnected modes

Tento balík odporúčame kombinovať s 18-palcovými diskmi z ľahkej zliatiny Turini v čiernej matnej farbe a s dekoratívnou lištou predného nárazníka v lesklej čiernej

Table 2 in Appendix I reports descriptive statistics of various indicators such as infant mortality rate, literacy rate (total), female literacy rate, mother’s education, poverty and

In the Snowpeople world, we wrote program code where the snowman tried to get the attention of a snowwoman?. All the program code was written in this one method, see

[r]

Manual control shaft set screw Sprag rod support bolts Idler shaft lock plate bolt Bearing retainer bolts.. Oil assembly mounting bolts pump Converter

The keyword static allows main() to be called without having to instantiate a particular instance of the class this is necessary because main() is called by Java virtual machine