1.初识对象
# 1.设计一个类
class Student:
name = None
gender = None
nationality = None
native_place = None
age = None
# 2.创建一个对象
stu_1 = Student()
# 3.对象属性进行赋值
stu_1.name = "张三"
stu_1.gender = "男"
stu_1.nationality = "中国"
stu_1.native_place = "山东省"
stu_1.age = 15
# 4.获取对象中记录的信息
print(stu_1.name)
2.类的成员方法
"""
演示面向对象类中的成员方法定义和使用
"""
# 定义一个带有成员方法的类
class Student:
name = None # 学生的姓名
def say_hi(self):
print(f"大家好,我是{self.name},请大家多多关照")
def say_hi2(self, msg):
print(f"大家好,我是{self.name},{msg}")
stu_1 = Student()
stu_1.name = "张三"
stu_1.say_hi()
stu_2 = Student()
stu_2.name = "李四"
stu_2.say_hi()
stu_3 = Student()
stu_3.name = "李四"
stu_3.say_hi2("ddd")
3.类和对象
"""
演示类和对象的关系,即面向对象的编程套路(思想)
"""
# 设计一个闹钟类
class Clock:
id = None # 序列化
price = None # 价格
def ring(self):
import winsound
winsound.Beep(2000, 3000)
clock1 = Clock()
clock1.id = "5465"
clock1.price = 21
print(f"闹钟的编号是:{clock1.id},闹钟的价格是:{clock1.price}")
clock1.ring()
4.构造方法
"""
演示类的构造方法
"""
# 演示使用构造方法对成员变量进行赋值
# 构造方法的名称: __init__
class Student:
# name = None
# age = None
# tel = None
def __init__(self, name, age, tel):
self.name = name
self.age = age
self.tel = tel
print("Student类创建了一个类对象")
stu = Student("张三", "45", "845465465")
print(stu.name)
print(stu.tel)
5.魔术方法
"""
演示类的魔术方法
"""
class Student:
def __init__(self, name, age):
self.name = name
self.age = age
# __str__魔术方法
def __str__(self):
return f"Student类对象,name:{self.name},age:{self.age}"
# __lt__魔术方法
def __lt__(self, other):
return self.age < other.age
# __le__魔术方法
def __le__(self, other):
return self.age <= other.age
# __eq__魔术方法
def __eq__(self, other):
return self.age == other.age
stu1 = Student("张三", 45)
stu2 = Student("李四", 36)
print(stu1)
print(stu1 < stu2)
print(stu1 <= stu2)
print(stu1 == stu2)
6.封装
面向对象编程,是许多编程语言都支持的一种编程思想。
简单理解是:基于模板(类)去创建实体(对象),使用对象完成功能开发
面向对象包含3大主要特性:
封装
继承
多态
"""
演示面向对象封装思想中私有成员的使用
"""
# 定义一个类,内含私有成员变量和私有成员方法
class Phone:
__current_voltage = 0.5 # 当前手机运行电压
def __keep_single_core(self):
print("让CPU以单核模式运行")
def call_by_5G(self):
if self.__current_voltage >= 1:
print("5G通话已开启")
else:
self.__keep_single_core()
print("电量不足")
phone = Phone()
# phone.__keep_single_core()
# print(phone.__current_voltage)
phone.call_by_5G()
7.继承
7.1继承的基础语法
多继承注意事项:
多个父类中,如果有同名的成员,那么默认以继承顺序(从左到右)为优先级。即:先继承的保留,后继承的被覆盖
"""
演示面向对象:继承的基础语法
"""
# 演示单继承
class Phone:
IMEI = None # 序列号
producer = "ITCAST" # 厂商
def call_by_4g(self):
print("4g通话")
class Phone2022(Phone):
face_id = "1654"
def call_by_5g(self):
print("2022新功能:5G通话")
phone = Phone2022()
print(phone.producer)
phone.call_by_4g()
phone.call_by_5g()
# 演示多继承
class NFCReader:
nfc_type = "第五代"
producer = "HM"
def read_card(self):
print("NFC读卡")
def write_card(self):
print("NFC写卡")
class RemoteControl:
rc_type = "红外遥控"
def control(self):
print("红外遥控开启")
class MyPhone(Phone, NFCReader, RemoteControl):
pass
phone = MyPhone()
phone.call_by_4g()
phone.control()
print(phone.producer)
7.2复写
子类继承父类的成员属性和成员方法后,如果对其“不满意”,那么可以进行复写。即:在子类中重新定义同名的属性或方法即可
"""
演示继承中的复写和使用父类成员
"""
class Phone:
IMEI = None # 序列号
producer = "ITCAST" # 厂商
def call_by_5g(self):
print("使用5G网络进行通话")
# 定义子类,复写父类成员
class MyPhone(Phone):
producer = "ITHEIMA" # 复写父类的成员属性
def call_by_5g(self):
print("开启CPU单核")
# print("使用5G网络进行通话")
# 方式 1
# print(f"父类的厂商是:{Phone.producer}")
# Phone.call_by_5g("self") 不可用
# 方式 2
print(f"父类的厂商是:{super().producer}")
super().call_by_5g()
Phone = MyPhone()
print(Phone.producer)
Phone.call_by_5g()
# 在子类中,调用父类成员
8.类型注解
8.1变量的类型注解
"""
演示变量的类型注解
"""
import json
import random
# 基础数据类型注解
var_1: int = 10
var_2: str = "itheima"
var_3: bool = True
# 类对象类型注解
class Student:
pass
stu: Student = Student()
# 基础容器类型注解
my_list: list = [1, 2, 3]
my_tuple: tuple = (1, 2, 3)
my_dict: dict = {"itheima":46}
# 容器类型详细注解
my_list1: list[int] = [1, 2, 3]
my_tuple1: tuple[int, str, bool] = (1, "ssda", True)
my_dict1: dict[str, int] = {"itheima":46}
# 在注释中进行类型注解
var_11 = random.randint(1, 10) # type: int
var_22 = json.loads('{"name":"zhangsan"') # type: dict[str, str]
def func():
return 10
var_33 = func() # type: int
8.2函数(方法)的类型注释
"""
演示对函数(方法)进行类型注解
"""
# 对形参进行类型注解
def add(x: int, y: int):
return x+y
# 对返回值进行类型注解
def func(data: list) -> list:
return data
8.3Union类型
"""
演示 union 联合类型注释
"""
# 使用 union 类型,必须先导包
from typing import Union
my_list: list[Union[int, str]] = [1, 2, "itheima", "itcast"]
def func(data: Union[int, str]) -> Union[int, str]:
pass
9.多态
多态,指的是:多种状态,即完成某个行为时,使用不同的对象会得到不同的状态
"""
演示面向对象的多态特性以及抽象类(接口)的使用
"""
class Animal:
def speak(self):
pass
class Dog(Animal):
def speak(self):
print("汪汪汪")
class Cat(Animal):
def speak(self):
print("喵喵喵")
def make_noise(animal: Animal):
"""制造点噪声,需要传入Animal对象"""
animal.speak()
# 演示多态,使用2个子类对象来调用函数
dog = Dog()
cat = Cat()
make_noise(dog)
make_noise(cat)
# 演示抽象类
class AC:
def cool_wind(self):
"""制冷"""
pass
def hot_wind(self):
"""制热"""
pass
def swing_l_r(self):
"""左右摆风"""
pass
class Midea_AC(AC):
def cool_wind(self):
print("美的空调制冷")
def hot_wind(self):
print("美的空调制热")
def swing_l_r(self):
print("美的空调左右摆风")
class GREE_AC(AC):
def cool_wind(self):
print("格力空调制冷")
def hot_wind(self):
print("格力空调制热")
def swing_l_r(self):
print("格力空调左右摆风")
def make_cool(ac:AC):
ac.cool_wind()
midea_ac = Midea_AC()
gree_ac = GREE_AC()
midea_ac.cool_wind()
gree_ac.cool_wind()