# 成员方法
# 类的定义和使用
# 介绍
- class 是关键字,表示要定义类了
- 类的属性,即定义在类中的变量(成员变量)
- 类的行为,即定义在类中的函数(成员方法)
# 代码
| class Student: |
| name = None |
| age = None |
| def say_hi(self): |
| print(f"{self.name}") |
| stu = Student() |
| stu.name = "周杰伦" |
| stu.say_hi() |
# 成员方法的定义语法
# 介绍
- self 关键字是成员方法定义必须填写的
- self 表示类对象自身的意思
# 代码
| class Student: |
| name = None |
| age = None |
| def say_hi(self, msg): |
| print(f"{self.name},{msg}") |
| stu = Student() |
| stu.name = "周杰伦" |
| stu.say_hi("baozi") |
# 类和对象
# 类和对象定义
基于类创建对象的语法: 对象名 = 类名称()
# 类定义
| class Student: |
| name = None |
| age = None |
| stu = Student() |
# 构造方法
# 属性(成员变量)赋值
| class Student: |
| name = None |
| age = None |
| tel = None |
| student = Student() |
| student.name = "baozi" |
| student.age = 21 |
| student.tel = "15831545456" |
# 构造方法
# 介绍
Python 类可以使用:init () 方法,称之为构造方法
- 在创建类对象(构造类)的时候,会自动执行
- 在创建类对象(构造类)的时候,将传入参数自动传递给__init__方法使用
# 代码
| class Student: |
| name = None |
| age = None |
| tel = None |
| del __init__(self, name, age, tel): |
| self.name = name |
| self.age = age |
| self.tel = tel |
| print("Student类创建了一个对象") |
| stu = Student("baozi", 21, "15648787897") |
# 其它内置方法
# __str__字符串方法
| class Student: |
| name = None |
| age = None |
| tel = None |
| del __init__(self, name, age, tel): |
| self.name = name |
| self.age = age |
| self.tel = tel |
| del __str__(self): |
| return "{self.name},{self.age},{self.tel}" |
| student = Student("baozi", 21, "15831454546") |
| print(student) |
# __lt__小于符号比较方法
| class Student: |
| name = None |
| age = None |
| tel = None |
| del __init__(self, name, age, tel): |
| self.name = name |
| self.age = age |
| self.tel = tel |
| del __lt__(self, other): |
| return self.age < other.age |
| student1 = Student("baozi", 21, "15831454546") |
| student2 = Student("baobao", 18, "15545456548") |
| print(student1 < student2) |
# __le__小于等于比较符号方法
| class Student: |
| name = None |
| age = None |
| tel = None |
| del __init__(self, name, age, tel): |
| self.name = name |
| self.age = age |
| self.tel = tel |
| del __le__(self, other): |
| return self.age <= other.age |
| student1 = Student("baozi", 21, "15831454546") |
| student2 = Student("baobao", 18, "15545456548") |
| print(student1 <= student2) |
# __eq__比较运算符实现
| class Student: |
| name = None |
| age = None |
| tel = None |
| del __init__(self, name, age, tel): |
| self.name = name |
| self.age = age |
| self.tel = tel |
| del __eq__(self, other): |
| return self.age == other.age |
| student1 = Student("baozi", 21, "15831454546") |
| student2 = Student("baobao", 18, "15545456548") |
| print(student1 == student2) |
# 封装
# 面向对象三大特征
面向对象编程,是很多编程语言都支持的一种编程思想
- 封装
- 继承
- 多态
# 封装介绍
封装表示的是,将现实世界事务的属性,行为封装到类中,描述为成员变量和成员方法
# 私有成员
# 私有成员变量
变量名以__开头
| class Phone: |
| IMET = None |
| producer = None |
| __current_voltage = None |
# 私有成员方法
方法名以__开头
| class Phone: |
| IMET = None |
| producer = None |
| __current_voltage = None |
| def __keep_single_core(self): |
| print("私有成员方法") |
# 使用私有成员
| class Phone: |
| IMET = None |
| producer = None |
| __current_voltage = None |
| def __keep_single_core(self): |
| print("私有成员方法") |
| phone = Phone() |
| phone.__current_voltage = 33 |
| phone.__keep_single_core() |
# 继承
# 单继承
| class Phone: |
| IMET = None |
| producer = None |
| def call_by_4g(self): |
| print("4g通话") |
| class Phone2023(Phone): |
| face_id = True |
| def call_by_5g(self): |
| pring("2023最新5g") |
# 多继承
| class Phone: |
| IMET = None |
| producer = None |
| def call_by_4g(self): |
| print("4g通话") |
| class NFCReader: |
| nfc_type = "第五代" |
| producer = "HM" |
| class Phone2023(Phone, NFCReader): |
| face_id = True |
| def call_by_5g(self): |
| pring("2023最新5g") |
先继承的保留,后继承的覆盖
# 复写
子类继承父类的成员属性和成员方法后,如果对其 “不满意”,那么可以进行复写
| class Phone: |
| IMET = None |
| producer = None |
| def call_by_5g(self): |
| print("父类5g通话") |
| class Phone2023(Phone): |
| face_id = True |
| def call_by_5g(self): |
| pring("子类2023最新5g") |
# 调用父类同名成员
一旦复写父类成员,那么类对象调用成员的时候,就会调用复写后的新成员
如果需要使用被复写的父类的成员,需要特殊的调用方式:
- 方式 1
- 调用父类成员
- 使用成员变量:父类名。成员变量
- 使用成员方法:父类名。成员方法 (self)
- 方式 2
- 使用 super () 调用父类成员
- 使用成员变量:super (). 成员变量
- 使用成员方法:super (). 成员方法 ()
# 类型注解
# 类型注解定义
在代码中涉及数据交互的地方,提供数据类型的注解(显式的说明)
- 帮助第三方 IDE 工具(如 PyCharm)对代码进行类型推断,协助做代码提示
- 帮助开发者自身对变量进行类型注释
- 变量的类型注解
- 函数(方法)形参列表和返回值的类型注解
# 类型注解的语法
# 基础数据类型注解
var_1: int = 10
var_2: float = 3.14159
var_3: bool = True
var_4: str = "baozi"
# 类对象类型注解
| class Student: |
| pass |
| stu: Student = Student() |
# 基础容器类型注解
| my_list: list = [1, 2 ,3] |
| my_tuple: tuple = (1, 2, 3) |
| my_set: set = {1, 2, 3} |
| my_dict: dict = {"baozi": 666} |
| my_str: str = "baozi" |
# 容器类型详细注解
| my_list: list[int] = [1, 2 ,3] |
| my_tuple: tuple[str, int, bool] = ("baozi", 2, True) |
# 函数(方法)的类型注解
# 形参注解
| def add(x: int, y: int): |
| return x + y |
# 返回值注解
| def add(x: int, y: int) -> int: |
| return x + y |
# 多态
# 多态介绍
多种状态,即完成某个行为时,使用不同的对象会得到不同的状态
| class Animal: |
| def speak(self): |
| pass |
| class Dog(Animal): |
| def speak(self): |
| print("汪汪汪") |
| class Cat(Animal): |
| def speak(self): |
| print("喵喵喵") |
| dog = Dog() |
| cat = Cat() |
| dog.speak() |
| cat.speak() |
# 抽象类(接口)
- 抽象类:含有抽象方法的类称之为抽象类
- 抽象方法:方法体是空实现的(pass)称之为抽象方法