1. 面向对象编程(OOP)
代码示例1:类的定义与使用
class Student:
name = None
age = None
tel = None
Student1 = Student()
Student1.name = "jkh"
Student1.age = "htfrv"
代码示例2:构造方法 __init__
和实例方法
class student:
def __init__(self, name, age):
self.name = name
self.age = age
stu1 = student("jyfg", 11)
stu2 = student("jkgh", 23)
代码示例4:类的继承与方法重写
class Phone:
IMET = phone
produser = None
def call_by_5g(self):
print("jkgh")
class MyPhone(Phone):
produser = "jhg" # 重写父类属性
def call_by_5g(self): #重写父类方法
print("kjghkj")
代码示例5:多继承
class NFcReader:
nfc_type = "jhg"
produser = "iugh"
def read_card(self):
print("juh")
def read_write(self):
print("juh")
class Remotecontro1:
rc_type = "jhyfyt"
def control(self):
print("jyhfgu")
class MyPhone(Phone, NFcReader, Remotecontro1):
pass
代码示例6:多态
def make_noise(animal: Animal):
animal.speak()
dog = Dog()
cat = Cat()
make_noise(dog)
make_noise(cat)
2. 类型注解
代码示例7:变量和集合类型注解
var_1: int = 10
var_2: float = 134.43
stu: student = student()
my_list: list[int] = [1, 2, 3]
my_tuple: tuple[set, int, bool] = ("kjhu", 555, True)
my_set: set[int] = {1, 2, 3}
代码示例8:联合类型 Union
注解
from typing import Union
my_list: list[Union[int, str]] = [1, 2, "itcast", "itheima"]
my_dict: dict[str, Union[str, int]] = {"jhg": "jhg", "jhg": 31}
def func(data: Union[int, str]) -> Union[int, str]:
pass
3. 随机数生成
代码示例9:生成随机数
import random
var_1 = random.randint(1, 10)
4. JSON 数据解析
代码示例10:JSON 数据解析
import json
var_2 = json.loads(data)
5. 错误示例
代码示例11:有错误的代码
# 修正前
stu = say_hi("jhgiukjnl")
# 修正后
class student:
def __init__(self, name, age):
self.name = name
self.age = age
stu1 = student("jyfg", 11)
stu2 = student("jkgh", 23)
print(stu1 < stu2)
在这个代码块中,存在多处错误,如 say_hi
函数的调用方式不正确,student
类定义中缩进错误等。
总结
- 面向对象编程(OOP):类的定义与使用、构造方法
__init__
和实例方法、重载__eq__
方法、类的继承与方法重写、多继承、多态。 - 类型注解:变量和集合类型注解、联合类型
Union
注解。 - 随机数生成:生成随机数。
- JSON 数据解析:解析 JSON 数据。
- 错误示例:有错误的代码示例,需要修正。
6. 多进程
代码示例1:基础多进程
import multiprocessing
import time
def sing():
for i in range(3):
print("i am sing ooo~")
time.sleep(0.5)
def dance():
for i in range(3):
print("i am dance lll~")
time.sleep(0.5)
if __name__ == '__main__':
sing_process = multiprocessing.Process(target=sing)
dance_process = multiprocessing.Process(target=dance)
sing_process.start()
dance_process.start()
代码示例2:带参数的多进程
import multiprocessing
import time
def sing(num, name):
for i in range(num):
print(name)
print("---i am sing ooo~")
time.sleep(0.5)
def dance(num, name):
for i in range(num):
print(name)
print("i am dance lll~")
time.sleep(0.5)
if __name__ == '__main__':
sing_process = multiprocessing.Process(target=sing, args=(3, "猪猪"))
dance_process = multiprocessing.Process(target=dance, kwargs={"name": "珊珊", "num": 2})
sing_process.start()
dance_process.start()
7. 多线程
代码示例3:基础多线程
import threading
import time
def sing():
for i in range(3):
print("i am sing ooo~")
time.sleep(0.5)
def dance():
for i in range(3):
print("i am dance lll~")
time.sleep(0.5)
if __name__ == '__main__':
sing_thread = threading.Thread(target=sing)
dance_thread = threading.Thread(target=dance)
sing_thread.start()
dance_thread.start()
代码示例4:带参数的多线程
import threading
import time
def sing(num):
for i in range(num):
print("i am sing ooo~")
time.sleep(0.5)
def dance(num):
for i in range(num):
print("i am dance lll~")
time.sleep(0.5)
if __name__ == '__main__':
sing_thread = threading.Thread(target=sing, args=(3,))
dance_thread = threading.Thread(target=dance, kwargs={"num": 2})
sing_thread.start()
dance_thread.start()
8. 线程管理
代码示例5:线程管理
import threading
import time
def task(thread_name):
print(f"Thread {thread_name} is running")
time.sleep(2)
print(f"Thread {thread_name} is done")
thread1 = threading.Thread(target=task, args=("Thread-1",))
thread2 = threading.Thread(target=task, args=("Thread-2",))
thread1.start()
thread2.start()
thread1.join()
thread2.join()
print("All threads have finished")
9. 内存管理
代码示例6:引用计数与垃圾回收
a = [1,2,3]
b = a
del a
del b
print(a) # 这行代码会报错,因为a已经被删除
总结
- 多进程:代码示例1和示例2展示了如何使用
multiprocessing
模块创建并启动多进程任务,以及如何传递参数。 - 多线程:代码示例3和示例4展示了如何使用
threading
模块创建并启动多线程任务,以及如何传递参数。 - 线程管理:代码示例5展示了如何创建、启动和管理线程,包括等待线程结束(
join
方法)。 - 内存管理:代码示例6展示了 Python 中引用计数和垃圾回收机制的简单示例。
通过这样的分类和说明,你可以更清楚地理解每段代码的功能和用途。