• Python 使用类的属性和方法


    1. 类的定义

    在Python中,类是通过关键字class来定义的。类是一种自定义的数据结构,可以包含属性(数据)和方法(函数)。下面是一个简单的类的定义:

    1. class Person:
    2. pass

    这个例子中,定义了一个名为Person的类,但没有包含任何属性和方法。接下来,我们将为这个类添加属性和方法。

    2. 属性的定义与使用

    属性是类的变量,用于存储对象的状态。在Python中,属性可以在类的内部定义,也可以在类的外部动态添加。

    定义属性

    我们可以在类的__init__方法中定义属性。__init__方法是一个特殊的方法,在创建对象时自动调用,用于初始化对象的属性。

    1. class Person:
    2. def __init__(self, name, age):
    3. self.name = name
    4. self.age = age

    在这个例子中,Person类有两个属性:nameage,它们在对象创建时被初始化。

    使用属性

    可以通过实例化类来创建对象,并访问和修改对象的属性:

    1. # 创建对象
    2. p = Person("Alice", 30)
    3. # 访问属性
    4. print(p.name) # 输出: Alice
    5. print(p.age) # 输出: 30
    6. # 修改属性
    7. p.age = 31
    8. print(p.age) # 输出: 31

    动态添加属性

    Python允许在类的外部动态添加属性:

    1. p.gender = "Female"
    2. print(p.gender) # 输出: Female

    3. 方法的定义与使用

    方法是类的函数,用于定义对象的行为。方法可以在类的内部定义,并可以访问和修改对象的属性。

    定义方法

    我们可以在类中定义方法,方法的第一个参数通常是self,用于引用调用该方法的对象。

    1. class Person:
    2. def __init__(self, name, age):
    3. self.name = name
    4. self.age = age
    5. def greet(self):
    6. print(f"Hello, my name is {self.name} and I am {self.age} years old.")

    使用方法

    可以通过对象调用类的方法:

    1. p = Person("Alice", 30)
    2. p.greet() # 输出: Hello, my name is Alice and I am 30 years old.

    4. 类的继承与多态

    继承是面向对象编程的一个重要概念,它允许一个类继承另一个类的属性和方法。多态是指同一个方法在不同的类中有不同的实现。

    定义子类

    子类可以继承父类的属性和方法,并可以添加自己的属性和方法。

    1. class Student(Person):
    2. def __init__(self, name, age, student_id):
    3. super().__init__(name, age)
    4. self.student_id = student_id
    5. def study(self):
    6. print(f"{self.name} is studying.")

    使用子类

    子类可以访问父类的属性和方法,并使用自己的属性和方法:

    1. s = Student("Bob", 20, "S12345")
    2. s.greet() # 输出: Hello, my name is Bob and I am 20 years old.
    3. s.study() # 输出: Bob is studying.

    多态

    多态允许我们使用父类引用来调用子类的方法:

    1. def introduce(person):
    2. person.greet()
    3. p = Person("Alice", 30)
    4. s = Student("Bob", 20, "S12345")
    5. introduce(p) # 输出: Hello, my name is Alice and I am 30 years old.
    6. introduce(s) # 输出: Hello, my name is Bob and I am 20 years old.

    5. 类的特殊方法与运算符重载

    特殊方法是Python类的一些特定方法,以双下划线开头和结尾。常见的特殊方法有__init____str____repr____eq__等。这些方法可以用于运算符重载,使对象可以使用内置运算符。

    定义特殊方法

    1. class Person:
    2. def __init__(self, name, age):
    3. self.name = name
    4. self.age = age
    5. def __str__(self):
    6. return f"Person(name={self.name}, age={self.age})"
    7. def __eq__(self, other):
    8. return self.name == other.name and self.age == other.age

    使用特殊方法

    1. p1 = Person("Alice", 30)
    2. p2 = Person("Alice", 30)
    3. p3 = Person("Bob", 20)
    4. print(p1) # 输出: Person(name=Alice, age=30)
    5. print(p1 == p2) # 输出: True
    6. print(p1 == p3) # 输出: False

    6. 类的封装、访问控制和装饰器

    封装是面向对象编程的一个重要概念,通过将属性和方法封装在类内部,可以实现对数据的保护。Python没有严格的访问控制,但通过约定可以实现类似的效果。

    私有属性和方法

    通过在属性和方法名前加双下划线,可以将其设为私有:

    1. class Person:
    2. def __init__(self, name, age):
    3. self.__name = name
    4. self.__age = age
    5. def __greet(self):
    6. print(f"Hello, my name is {self.__name} and I am {self.__age} years old.")

    访问私有属性和方法

    私有属性和方法不能在类的外部直接访问,但可以通过类内部的方法间接访问:

    1. class Person:
    2. def __init__(self, name, age):
    3. self.__name = name
    4. self.__age = age
    5. def greet(self):
    6. self.__greet()
    7. def __greet(self):
    8. print(f"Hello, my name is {self.__name} and I am {self.__age} years old.")
    9. p = Person("Alice", 30)
    10. p.greet() # 输出: Hello, my name is Alice and I am 30 years old.

    装饰器

    装饰器是用于修改函数或方法行为的函数。常见的类方法装饰器有@staticmethod@classmethod@property

    静态方法

    静态方法不需要访问实例,可以通过类名直接调用:

    1. class Person:
    2. @staticmethod
    3. def is_adult(age):
    4. return age >= 18
    5. print(Person.is_adult(20)) # 输出: True
    类方法

    类方法可以访问类属性和类方法,通过@classmethod装饰:

    1. class Person:
    2. species = "Homo sapiens"
    3. @classmethod
    4. def get_species(cls):
    5. return cls.species
    6. print(Person.get_species()) # 输出: Homo sapiens
    属性方法

    属性方法可以将方法转换为属性,通过@property装饰:

    1. class Person:
    2. def __init__(self, name, age):
    3. self.__name = name
    4. self.__age = age
    5. @property
    6. def name(self):
    7. return self.__name
    8. @name.setter
    9. def name(self, name):
    10. self.__name = name
    11. p = Person("Alice", 30)
    12. print(p.name) # 输出: Alice
    13. p.name = "Bob"
    14. print(p.name) # 输出: Bob

    7. 类的属性和方法的实践案例

    实现一个简单的银行账户类

    下面我们通过一个简单的银行账户类来总结上述内容。

    1. class BankAccount:
    2. def __init__(self, owner, balance=0):
    3. self.owner = owner
    4. self.__balance = balance
    5. def deposit(self, amount):
    6. if amount > 0:
    7. self.__balance += amount
    8. print(f"Deposited {amount}. New balance is {self.__balance}.")
    9. else:
    10. print("Deposit amount must be positive.")
    11. def withdraw(self, amount):
    12. if 0 < amount <= self.__balance:
    13. self.__balance -= amount
    14. print(f"Withdrew {amount}. New balance is {self.__balance}.")
    15. else:
    16. print("Insufficient funds or invalid amount.")
    17. @property
    18. def balance(self):
    19. return self.__balance
    20. @balance.setter
    21. def balance(self, amount):
    22. print("Balance cannot be set directly. Use deposit or withdraw methods.")
    23. def __str__(self):
    24. return f"BankAccount(owner={self.owner}, balance={self.__balance})"
    25. # 使用银行账户类
    26. account = BankAccount("Alice", 1000)
    27. print(account) # 输出: BankAccount(owner=Alice, balance=1000)
    28. account.deposit(500) # 输出: Deposited 500. New balance is 1500.
    29. account.withdraw(200) # 输出: Withdrew 200. New balance is 1300.
    30. print(account.balance) # 输出: 1300
    31. account.balance = 2000 # 输出: Balance cannot be set directly. Use deposit or withdraw methods.

    在这个例子中,BankAccount类定义了账户所有者和账户余额两个属性,并提供了存款、取款的方法。通过使用私有属性和属性方法,保护了账户余额的直接访问,同时提供了存取款操作。

  • 相关阅读:
    【数据结构】归并排序
    字符串定义
    Jackson 代码示例
    程序员找副业有哪几个方向(纯干货)
    Linux内核的基本工作原理和关键概念
    Eureka详解
    Busco-真核生物为主基因组质量评估
    Java8 使用 stream().filter()过滤List对象(查找符合条件的对象集合)
    QTcpSocket网络通讯多客户端功能
    【Python计算机视觉】Python全栈体系(二十四)
  • 原文地址:https://blog.csdn.net/Itmastergo/article/details/140461410