在Python中,类是通过关键字class来定义的。类是一种自定义的数据结构,可以包含属性(数据)和方法(函数)。下面是一个简单的类的定义:
- class Person:
- pass
这个例子中,定义了一个名为Person的类,但没有包含任何属性和方法。接下来,我们将为这个类添加属性和方法。
属性是类的变量,用于存储对象的状态。在Python中,属性可以在类的内部定义,也可以在类的外部动态添加。
我们可以在类的__init__方法中定义属性。__init__方法是一个特殊的方法,在创建对象时自动调用,用于初始化对象的属性。
- class Person:
- def __init__(self, name, age):
- self.name = name
- self.age = age
在这个例子中,Person类有两个属性:name和age,它们在对象创建时被初始化。
可以通过实例化类来创建对象,并访问和修改对象的属性:
- # 创建对象
- p = Person("Alice", 30)
-
- # 访问属性
- print(p.name) # 输出: Alice
- print(p.age) # 输出: 30
-
- # 修改属性
- p.age = 31
- print(p.age) # 输出: 31
Python允许在类的外部动态添加属性:
- p.gender = "Female"
- print(p.gender) # 输出: Female
方法是类的函数,用于定义对象的行为。方法可以在类的内部定义,并可以访问和修改对象的属性。
我们可以在类中定义方法,方法的第一个参数通常是self,用于引用调用该方法的对象。
- class Person:
- def __init__(self, name, age):
- self.name = name
- self.age = age
-
- def greet(self):
- print(f"Hello, my name is {self.name} and I am {self.age} years old.")
可以通过对象调用类的方法:
- p = Person("Alice", 30)
- p.greet() # 输出: Hello, my name is Alice and I am 30 years old.
继承是面向对象编程的一个重要概念,它允许一个类继承另一个类的属性和方法。多态是指同一个方法在不同的类中有不同的实现。
子类可以继承父类的属性和方法,并可以添加自己的属性和方法。
- class Student(Person):
- def __init__(self, name, age, student_id):
- super().__init__(name, age)
- self.student_id = student_id
-
- def study(self):
- print(f"{self.name} is studying.")
子类可以访问父类的属性和方法,并使用自己的属性和方法:
- s = Student("Bob", 20, "S12345")
- s.greet() # 输出: Hello, my name is Bob and I am 20 years old.
- s.study() # 输出: Bob is studying.
多态允许我们使用父类引用来调用子类的方法:
- def introduce(person):
- person.greet()
-
- p = Person("Alice", 30)
- s = Student("Bob", 20, "S12345")
-
- introduce(p) # 输出: Hello, my name is Alice and I am 30 years old.
- introduce(s) # 输出: Hello, my name is Bob and I am 20 years old.
特殊方法是Python类的一些特定方法,以双下划线开头和结尾。常见的特殊方法有__init__、__str__、__repr__、__eq__等。这些方法可以用于运算符重载,使对象可以使用内置运算符。
- class Person:
- def __init__(self, name, age):
- self.name = name
- self.age = age
-
- def __str__(self):
- return f"Person(name={self.name}, age={self.age})"
-
- def __eq__(self, other):
- return self.name == other.name and self.age == other.age
- p1 = Person("Alice", 30)
- p2 = Person("Alice", 30)
- p3 = Person("Bob", 20)
-
- print(p1) # 输出: Person(name=Alice, age=30)
- print(p1 == p2) # 输出: True
- print(p1 == p3) # 输出: False
封装是面向对象编程的一个重要概念,通过将属性和方法封装在类内部,可以实现对数据的保护。Python没有严格的访问控制,但通过约定可以实现类似的效果。
通过在属性和方法名前加双下划线,可以将其设为私有:
- class Person:
- def __init__(self, name, age):
- self.__name = name
- self.__age = age
-
- def __greet(self):
- print(f"Hello, my name is {self.__name} and I am {self.__age} years old.")
私有属性和方法不能在类的外部直接访问,但可以通过类内部的方法间接访问:
- class Person:
- def __init__(self, name, age):
- self.__name = name
- self.__age = age
-
- def greet(self):
- self.__greet()
-
- def __greet(self):
- print(f"Hello, my name is {self.__name} and I am {self.__age} years old.")
-
- p = Person("Alice", 30)
- p.greet() # 输出: Hello, my name is Alice and I am 30 years old.
装饰器是用于修改函数或方法行为的函数。常见的类方法装饰器有@staticmethod、@classmethod和@property。
静态方法不需要访问实例,可以通过类名直接调用:
- class Person:
- @staticmethod
- def is_adult(age):
- return age >= 18
-
- print(Person.is_adult(20)) # 输出: True
类方法可以访问类属性和类方法,通过@classmethod装饰:
- class Person:
- species = "Homo sapiens"
-
- @classmethod
- def get_species(cls):
- return cls.species
-
- print(Person.get_species()) # 输出: Homo sapiens
属性方法可以将方法转换为属性,通过@property装饰:
- class Person:
- def __init__(self, name, age):
- self.__name = name
- self.__age = age
-
- @property
- def name(self):
- return self.__name
-
- @name.setter
- def name(self, name):
- self.__name = name
-
- p = Person("Alice", 30)
- print(p.name) # 输出: Alice
- p.name = "Bob"
- print(p.name) # 输出: Bob
下面我们通过一个简单的银行账户类来总结上述内容。
- class BankAccount:
- def __init__(self, owner, balance=0):
- self.owner = owner
- self.__balance = balance
-
- def deposit(self, amount):
- if amount > 0:
- self.__balance += amount
- print(f"Deposited {amount}. New balance is {self.__balance}.")
- else:
- print("Deposit amount must be positive.")
-
- def withdraw(self, amount):
- if 0 < amount <= self.__balance:
- self.__balance -= amount
- print(f"Withdrew {amount}. New balance is {self.__balance}.")
- else:
- print("Insufficient funds or invalid amount.")
-
- @property
- def balance(self):
- return self.__balance
-
- @balance.setter
- def balance(self, amount):
- print("Balance cannot be set directly. Use deposit or withdraw methods.")
-
- def __str__(self):
- return f"BankAccount(owner={self.owner}, balance={self.__balance})"
-
- # 使用银行账户类
- account = BankAccount("Alice", 1000)
- print(account) # 输出: BankAccount(owner=Alice, balance=1000)
-
- account.deposit(500) # 输出: Deposited 500. New balance is 1500.
- account.withdraw(200) # 输出: Withdrew 200. New balance is 1300.
-
- print(account.balance) # 输出: 1300
- account.balance = 2000 # 输出: Balance cannot be set directly. Use deposit or withdraw methods.
在这个例子中,BankAccount类定义了账户所有者和账户余额两个属性,并提供了存款、取款的方法。通过使用私有属性和属性方法,保护了账户余额的直接访问,同时提供了存取款操作。
