Základy objektově orientovaného programování

Intermediate Object-Oriented Programming in Python

Jake Roach

Data Engineer

Definice třídy

class Person:
  def __init__(self, name, age):
    self.name = name
    self.age = age
    self.height = 0

# This invokes a call to __init__
john = Person("John Casey", 38)
  • Třídu definujeme klíčovým slovem class
  • __init__ je konstruktor, volaný při vytvoření nové instance třídy
  • self odkazuje na aktuální instanci třídy
Intermediate Object-Oriented Programming in Python

Atributy instance

class Person:
  def __init__(self, name, age):
    self.name = name
    self.age = age
# Create an instance of Person
sarah = Person("Sarah Walker", 31)
sarah.age  # Retrieve the age instance attribute
  • Jsou asociovány s instancí třídy
  • Lze je nastavit a získat pomocí syntaxe self.<attribute-name>
  • Přístup přes <object-name>.<attribute-name>
Intermediate Object-Oriented Programming in Python

Atributy třídy

  • Jsou vázány na třídu samotnou
  • Lze je získat bez instance třídy
  • Uchovávají data společná pro všechny instance třídy
class Person:
  residence = "Planet Earth"
  ...
# Accessed without an instance
print(Person.residence)
Planet Earth
Intermediate Object-Oriented Programming in Python

Metody instance

class Person:
  ...
  def introduce(self):
      print(f"Hello, my name is {self.name}")
chuck = Person("Chuck", 32)
chuck.introduce()  # Called on a Person object

$$

  • Vyžadují existující instanci třídy
  • Jako první parametr přijímají klíčové slovo self
Intermediate Object-Oriented Programming in Python

Metody třídy

class Person:
  @classmethod
  def wake_up(cls):
      print("Time to start your day!")
# Calling a class method
Person.wake_up()

$$

  • Označeny dekorátorem @classmethod
  • Nevyžadují instanci třídy
Intermediate Object-Oriented Programming in Python

Dědičnost

Dědičnost umožňuje opakované použití kódu mezi třídami

  • Podtřída (Employee) dědí veškerou funkcionalitu nadtřídy (Person)
  • Vztah „je"
  • Může implementovat další funkcionalitu
    • Atributy
    • Metody
class Person:
  def __init__(self, name, age):
    self.name
    self.age = age

  def introduce(self):
      print(f"Hello, my name is {self.name}")
class Employee(Person):
  def __init__(self, name, age, title):
    Person.__init__(self, name, age)
    self.title = title

  def change_position(self, new_title):
      self.title = new_title
Intermediate Object-Oriented Programming in Python

Dědičnost

lester = Employee("Lester", 26, "Technician")
lester.introduce()  # Inherited from Person
print(lester.title)
Hello, my name is Lester
Technician
lester.change_position("Cashier")
print(lester.title)
Cashier
Intermediate Object-Oriented Programming in Python

super()

class Employee(Person):
  def __init__(self, name, age, title):
    # Uses name of the parent class
    Person.__init__(self, name, age)

    self.title = title
  ...
  • Název třídy, __init__()

Použití super()

class Employee(Person):
  def __init__(self, name, age, title):
    # super(), instead of class name
    super().__init__(name, age)

    self.title = title
  ...
  • super(), __init__()
  • self není třeba předávat
Intermediate Object-Oriented Programming in Python

Přepisování metod

Podtřída implementuje zděděnou metodu novým způsobem

class Employee(Person):
  ...
  def introduce(self):
      print(f"""My name is {self.name},
        I am a {self.title}""")
lester = Employee("Lester", 26, "Technician")
lester.introduce()
My name is Lester, I am a Technician
Intermediate Object-Oriented Programming in Python

Přetěžování operátorů

class Person:
  def __init__(self, name):
      self.name = name

  def __eq__(self, other):
      return self.name == other.name

$$

  • Přizpůsobení chování operátorů Pythonu pro třídu
  • __eq__() slouží k přetížení operátoru ==

$$

$$

$$

$$

# Compare two Person objects
chuck = Person("Charles Carmichael")
charles = Person("Charles Carmichael")
print(chuck == charles)
True
Intermediate Object-Oriented Programming in Python

Pojďme procvičovat!

Intermediate Object-Oriented Programming in Python

Preparing Video For Download...