Python学习笔记(12)-Python进阶12-类和对象
发布日期:2021-05-09 15:16:32 浏览次数:14 分类:精选文章

本文共 3881 字,大约阅读时间需要 12 分钟。

Python������12-������������

���Python���������������������������������������������������������������������������������������������������������������������������������Python������������������������������������������������������������������������������������������������������������Python������������������������������������������������������

������������������������

������������������������������������������������������������������������������������������������������������������������������������������������������������������User���������������������������������������������������

class User:
def __init__(self, name, age):
self.name = name
self.age = age
self.email = None
def __str__(self):
return f"���������{self.name}���{self.age}���"
def send_email(self):
self.email = f"{self.name}'s email������"

���������User���������������������������������������User("������", 25)���������������������������������������������������������������������User������������������������������user.name���user.send_email()���

���������������������

������������������������������������������������������

  • ������������classmethod���������������������������������������������������@classmethod������������������������
  • ���������������instance variable���������������������������������������������������

���������������_cycle

������Python������������������������������������������������

  • __init__������������������������������
  • __del__������������������������������������������������������������
  • __ Exit__������������������������������������������
  • ���������������������������������������������������������������������������������������

    ������������������

    ���������������������������������������������Inheritance���������������������������������������������������������������������������������������������������������������������������������������

    class Animal:
    def make_sound(self):
    print("������������")
    def run(self):
    print("������������")
    class Dog(Animal):
    # ���������������������������
    def jump(self):
    print("���������")
    def run(self):
    super().run() # ������������run������
    print("���������")
    # ���������������������
    class Dog���������Animal������������������������������������������
    class Dog(Animal, AnotherClass):
    pass

    ���������Polymorphism������������������������������������������������Python������������������������������������������������������������������Animal������������������make_sound()������������������������������

    ���������������������

    ������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

    ������������������������

    ������������������������������������������������������������������������������������������������������������@staticmethod������������������������

    class Calculator:
    @staticmethod
    def add(a, b):
    return a + b
    Calculator.add(3, 5) # ������������
    user.calculator = Calculator()
    user.calculator.add(3,5) # ���������������

    ������������������������������������������������������������������������������

    ���������������������������

    Python��������������������������������������������������� ���������������������������������������������������������getattr()���������������vars()���������������������������������������������������

    ���������������������

    ���������������������������

  • ������������������������������������������������������UnboundLocalError���
  • ������������������������������������������������������������������
  • ������������������������������������������������������
  • ������

    ���������������Python���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

    上一篇:Python学习笔记(13)-Python进阶13-模块
    下一篇:Python学习笔记(11)-Python进阶11-函数

    发表评论

    最新留言

    第一次来,支持一个
    [***.219.124.196]2025年04月26日 15时01分30秒