python_day10_复写,类型注解

复写:重写父类属性

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().成员变量/方法
python_day10_复写,类型注解_第1张图片

python_day10_复写,类型注解_第2张图片

类型注解: 变量:类型

python_day10_复写,类型注解_第3张图片

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

python_day10_复写,类型注解_第4张图片

形参注解

def add(x: int, y: int):
    return x + y


add()

python_day10_复写,类型注解_第5张图片

对返回值注解

def function(data: list) -> list:
    return data


function()

python_day10_复写,类型注解_第6张图片

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()

python_day10_复写,类型注解_第7张图片

Ctrl+P 快捷键查看提示

python_day10_复写,类型注解_第8张图片

你可能感兴趣的:(python,python,开发语言)