# 构造方法 # 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("asd", 11, "123123") # # print(stu.tel) # print(stu.age) # print(stu.name) import json import random # 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 # # # stu = Student("joker", 32) # stu2 = Student("lei", 34) # stu3 = Student("fun", 34) # print(stu) # str # print(str(stu)) # str # print(stu > stu2) # 比较 # print(stu3 >= stu2) # 比较 # 私有成员变量与方法 # 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.call_by_5g() # # phone.keep_single_core() # 类型注解 # 基础注解 变量:类型 var: int = 2 my_list: list = [1, 2, 3] my_tuple: tuple[str, int, bool] = ("ithe", 2, False) my_dict: dict[str, int] = {"sadf": 123} var_2 = random.randint(1, 31) # type:int var_3 = json.loads('{"name":"zhangsan"}') # type:dict[str,str]