复写:重写父类属性
class Phone:
IMEI = None
producer = "XM"
def call_by_5g(self):
print("5g网络")
# 复写
class myPhone(Phone):
producer = "HUAWEI"
def call_by_5g(self):
print("复写")
# 调用父类成员:方式1
# print(f"父类的厂商{Phone.producer}")
# Phone.call_by_5g(self)
# 方式二
print(f"父类的厂商{super().producer}")
super().call_by_5g()
print("关闭5G")
phone = myPhone()
phone.call_by_5g()
print(phone.producer)
调用父类成员:
方式1: 父类名.成员变量/方法
方式2:super().成员变量/方法
类型注解: 变量:类型
var_1: int = 10
var_2: str = 'java'
var_3: bool = True
class Student:
pass
stu: Student = Student()
my_list: list[int] = [1, 2, 3]
my_tuple: tuple = (1, 2, 3)
my_dict: dict[str, int] = {"java": 1}
type: 类型
var_4 = 10 # type: int
var_5 = json.loads('{"name":"java"}') # type: dict[str,str]
def fun():
pass
var_6 = fun() # type: int
形参注解
def add(x: int, y: int):
return x + y
add()
对返回值注解
def function(data: list) -> list:
return data
function()
union联合注解,先导包
from typing import Union
my_list1: list[Union[int, str]] = [1, "c", 2]
def func(data: Union[int, str]) -> Union[int, str]:
pass
func()