• python 面向对象


    类的定义和使用

    使用对象组织数据

    1. #设计一个类(设计一张登记表)
    2. class student:
    3. name = None
    4. gender = None
    5. nationality = None
    6. native_place = None
    7. age = None
    8. #创建一个对象(打印一张登记表)
    9. stu_1 = student()
    10. #对象属性进行赋值(填写表单)
    11. stu_1.name = "林俊杰"
    12. stu_1.gender = "男"
    13. stu_1.nationality = "中国"
    14. stu_1.native_place = "山东省"
    15. stu_1.age = "31"
    16. #获取对象中记录的信息
    17. print(stu_1.name)
    18. print(stu_1.gender)
    19. print(stu_1.nationality)
    20. print(stu_1.native_place)
    21. print(stu_1.age)

    实例:定义闹钟并工作

    1. import winsound
    2. class Clock:
    3. def __init__(self, id, price):
    4. self.id = id
    5. self.price = price
    6. def ring(self):
    7. winsound.Beep(2000, 200)
    8. clock1 = Clock("001", 19.99)
    9. print(f"闹钟的id是: {clock1.id}, 价格是: {clock1.price}")
    10. clock1.ring()
    11. clock2 = Clock("002", 29.99)
    12. print(f"闹钟的id是: {clock2.id}, 价格是: {clock2.price}")
    13. clock2.ring()

    构造方法

    1. class student:
    2. def __init__(self,name,age,tel):
    3. self.name = name
    4. self.age = age
    5. self.tel = tel
    6. print("student类创建了一个类对象")
    7. stu = student("周杰伦",31,"10192837190")
    8. print(stu.name)
    9. print(stu.age)
    10. print(stu.tel)

    题目:

    1. class Student:
    2. def __init__(self, name, age, address):
    3. self.name = name
    4. self.age = age
    5. self.address = address
    6. # 创建一个空的学生表
    7. students = []
    8. # 通过for循环和input语句,逐个录入学生信息
    9. for i in range(10):
    10. print(f"请输入第{i+1}个学生的信息")
    11. name = input("姓名:")
    12. age = input("年龄:")
    13. address = input("地址:")
    14. # 实例化一个学生对象并添加到学生列表中
    15. student = Student(name, age, address)
    16. students.append(student)
    17. # 使用print语句输出所有学生信息
    18. print("学生信息如下:")
    19. for i, student in enumerate(students):
    20. print(f"第{i+1}个学生:")
    21. print(f"姓名:{student.name}")
    22. print(f"年龄:{student.age}")
    23. print(f"地址:{student.address}")
    24. print()

    魔术方法

    lt用于小于和大于

    1. 构造函数 __init__ 用于初始化学生的姓名和年龄属性。
    2. __str__ 方法返回一个以适当格式表示学生对象的字符串。
    3. __lt__ 方法是一个特殊方法,用于比较两个学生对象的年龄属性。在该方法中,我们使用 < 运算符来比较年龄的大小。
    4. __le__ 方法是一个特殊方法,用于比较两个学生对象的姓名属性。在该方法中,我们使用 <= 运算符来比较姓名的大小。
    5. __eq__ 方法是一个特殊方法,用于比较两个学生对象的姓名属性是否相等。在该方法中,我们使用 == 运算符来比较姓名的相等性。
    1. class student:
    2. def __init__(self, name, age):
    3. self.name = name
    4. self.age = age
    5. def __str__(self):
    6. return f"student的类对象:name{self.name},age{self.age}"
    7. def __lt__(self, other):
    8. return self.age < other.age
    9. def __le__(self, other):
    10. return self.name <= other.name
    11. def __eq__(self, other):
    12. return self.name == other.name
    13. stu1 = student("周杰伦", 31)
    14. stu2 = student("周杰伦", 31)
    15. print(stu1 == stu2)
    16. print(stu1 > stu2)

    封装

    1. #定义一个类
    2. from ast import If
    3. class phone:
    4. __current_voltage = 1 #当前手机电压
    5. def __keep_single_core(self):
    6. print("让cpu以单核模式运行")
    7. def call_by_5g(self):
    8. if self.__current_voltage >= 1:
    9. print("5G模式已开启")
    10. else:
    11. self.__keep_single_core()
    12. print("电量不足,无法使用5G通话,并设置为单核运行进行省电")
    13. phone = phone()
    14. phone.call_by_5g()

    题目:

    1. #设计一个类,用来描述手机
    2. class phone:
    3. #提供私有成员变量:__is_5g_enable
    4. __is_5g_enable = False #5g关闭
    5. #提供私有成员方法:__check_5g()
    6. def __check_5g(self):
    7. if self.__is_5g_enable:
    8. print("5g开启")
    9. else:
    10. print("5g关闭,使用4g")
    11. #提供公开成员方法:call_by_5g()
    12. def call_by_5g(self):
    13. self.__check_5g()
    14. print("正在通话中")
    15. phone = phone()
    16. phone.call_by_5g()
  • 相关阅读:
    ETCD集群搭建(实践可用)
    解读《生成式人工智能服务管理暂行办法》
    js判断对象是否为空
    Java字节码增强技术(NoClassDefFoundError)问题
    解锁前端Vue3宝藏级资料 第三章 Vue Router路由器的使用
    练习题37:命名空间练习
    如何应对访问国外服务器缓慢的问题?SDWAN组网是性价比之选
    webpack中tree shaking
    Kubernetes2
    信而泰自动化OSPFv2测试小技巧
  • 原文地址:https://blog.csdn.net/SENMINGya/article/details/133914173