面向对象编程基础

Python 面向对象编程进阶

Jake Roach

Data Engineer

定义类

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)
  • 使用 class 关键字定义类
  • __init__ 是构造函数,在创建新对象时调用
  • self 指向类的当前实例
Python 面向对象编程进阶

实例属性

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
  • 与类的对象关联
  • 使用 self.<attribute-name> 设置和获取
  • 通过 <object-name>.<attribute-name> 访问
Python 面向对象编程进阶

类属性

  • 绑定到类本身
  • 可在没有类实例时获取
  • 存储所有对象应相同的数据
class Person:
  residence = "Planet Earth"
  ...
# Accessed without an instance
print(Person.residence)
Planet Earth
Python 面向对象编程进阶

实例方法

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

$$

  • 需要先有类对象才能调用
  • 第一个参数为 self
Python 面向对象编程进阶

类方法

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

$$

  • @classmethod 装饰
  • 调用时不需要类实例
Python 面向对象编程进阶

继承

继承用于在类之间复用代码

  • 子类(Employee)继承父类(Person)的全部功能
  • "is-a" 关系
  • 可实现额外功能
    • 属性
    • 方法
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
Python 面向对象编程进阶

继承

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
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
  ...
  • 类名,__init__()

使用 super()

class Employee(Person):
  def __init__(self, name, age, title):
    # super(),而非类名
    super().__init__(name, age)

    self.title = title
  ...
  • super()__init__()
  • 不需要传入 self
Python 面向对象编程进阶

重写(Overriding)

子类以新的方式实现从父类继承的方法

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
Python 面向对象编程进阶

重载(Overloading)

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

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

$$

  • 为类自定义 Python 运算符的行为
  • __eq__() 用于重载 ==

$$

$$

$$

$$

# Compare two Person objects
chuck = Person("Charles Carmichael")
charles = Person("Charles Carmichael")
print(chuck == charles)
True
Python 面向对象编程进阶

Passons à la pratique !

Python 面向对象编程进阶

Preparing Video For Download...