Python Classes

T. Issaris

1 Maart 2021

Hergebruik code

Stel je wil de speler, vijanden en meteoren kunnen voorstellen in een spel:

class Player:
    def move(self, x, y):
    # ...

class Enemy:
    def move(self, x, y):
    # ...

class Meteor:
    def move(self, x, y):
    # ...

Herbruik code met inheritance

class Entity:
    def move(self, x, y):
    # ...

class Player(Entity):
    pass

class Enemy(Entity):
    pass

class Meteor(Entity):
    pass

Polymorfisme

class Entity:
    def move(self, x, y):
    # ...

class Player(Entity):
...
class Enemy(Entity):
...
spel_items = [Player(), Enemy(), Enemy()]
for item in spel_items::
    item.move(...)

Duck-typing

“If it walks like a duck and it quacks like a duck, then it must be a duck”

class Entity:
    def move(self, x, y):
    # ...

class Player(Entity):
...
class Enemy(Entity):
...
spel_items = [Player(), Enemy(), Enemy()]
for item in spel_items::
    item.move(...)

Duck-typing

class Duck:
    def fly(self):
        print("Duck flying")

class Whale:
    def swim(self):
        print("Whale swimming")

for animal in [Duck(), Whale()]:
    animal.fly()

Code duplicatie

class Hond:
    def maak_geluid(self, x):
        for i in range(x):
            print("Woef")

class Kat:
    def maak_geluid(self):
        for i in range(x):
            print("Miauw")

class Paard:
    def maak_geluid(self):
        for i in range(x):
            print("Huuuu")
h = Kat()
h.maak_geluid(2) # Miauw Miauw
h = Hond()
h.maak_geluid(3) # Woef Woef Woef

Code duplicatie vermijden met inheritance

class Dier:
    geluid = "?"
    def maak_geluid(self, x):
        for i in range(x):
            print(self.geluid)

class Hond(Dier):
    geluid = "Woef"

class Kat(Dier):
    geluid = "Miauw"

class Paard(Dier):
    geluid = "Huuuu"

h = Kat()
h.maak_geluid(2) # Miauw Miauw

Overriding methods

class Dier:
    geluid = "?"
    def maak_geluid(self):
        print(self.geluid)

class Mens(Dier):
    def maak_geluid(self):
        print("Ik maak geen geluiden, maar spreek.")

Overriding methods

class Block:
    value = 1
    def kap(self):
        print("ping")
        player_experience += value

class Diamant(Block):
    value = 10

class Dynamiet(Block):
    def kap(self):
        print("Boem!!!")
        player_life = 0

Overriding methods

class Block:
    value = 1
    def kap(self):
        print("ping")
        player_experience += value

class Dynamiet(Block):
    def kap(self):
        super().kap()
        print("Boem!!!")
        player_life = 0

Overriding methods

class Block:
    def __init__(self):
        print("ping")

class Dynamiet(Block):
    def __init__(self):
        print("boem")
d = Block() # ping

Overriding methods

class Block:
    def __init__(self):
        print("ping")

class Dynamiet(Block):
    def __init__(self):
        print("boem")
d = Dynamiet() # boem

Overriding methods

class Block:
    def __init__(self):
        print("ping")

class Dynamiet(Block):
    def __init__(self):
        super().__init__()
        print("boem")
d = Dynamiet() # ping boem
// reveal.js plugins