您的位置:首页 > 健康 > 养生 > 从零开始的python学习生活2

从零开始的python学习生活2

2024/10/6 22:24:16 来源:https://blog.csdn.net/satellite_s/article/details/140332088  浏览:    关键词:从零开始的python学习生活2

接上封装

class Phone:__volt=0.5def __keepsinglecore(self):print("让cpu以单核运行")def if5G(self):if self.__volt>=1:print("5G通话已开启")else:self.__keepsinglecore()print("电量不足,无法使用5G通话,已经设置为单核运行来省电")phone=Phone()
phone.if5G()

私有的成员变量和方法我们外部是无法使用的,但是内部的其他成员方法是可以使用的
在这里插入图片描述
练习题
在这里插入图片描述

class Phone:__is_5g_enable=Falsedef __check_5g(self):if self.__is_5g_enable==True:print("5g开启")else:print("5g关闭,使用4g网络")def call_by_5g(self):self.__check_5g()print("正在通话中")phone=Phone()
phone.call_by_5g()

在这里插入图片描述

继承

在这里插入图片描述
在这里插入图片描述
我们虽然新开发了面部识别功能,但是我们还是会继承之前已经有的功能
在这里插入图片描述
在这里插入图片描述

class Phone:id=Noneproducer="可是雪"def call_by_4g(self):print("4g通话")class Phone2024(Phone):face_id="10001"def call_by_5g(self):print("2024年新功能,5g通话")phone=Phone2024()
phone.call_by_5g()
phone.call_by_4g()
print(phone.producer)

在这里插入图片描述
继承的类的方法和变量都可以用
在这里插入图片描述
在这里插入图片描述
下面代码里面说了pass
我们这个类已经继承了很多类了,不需要再添加别的东西了,所以我们就可以直接写一个pass

class Phone:id=Noneproducer="可是雪"def call_by_4g(self):print("4g通话")class Phone2024(Phone):face_id="10001"def call_by_5g(self):print("2024年新功能,5g通话")phone=Phone2024()
phone.call_by_5g()
phone.call_by_4g()
print(phone.producer)class NFC:nfc_type="第五代"producer="可是雪"def read_card(self):print("NFC读卡")def write_card(self):print("NFC写卡")class RemoteControl:rc_type="红外遥控"def control(self):print("红外遥控开启了")class MyPhone(Phone2024,NFC,RemoteControl):pass
phone=MyPhone()
phone.call_by_4g()
phone.call_by_5g()
phone.read_card()
phone.write_card()
phone.control()

这里面能很清楚的看见,我们直接继承了Phone2024的,但是我们依旧可以使用phone里面的功能,也就是继承具有连续性
在这里插入图片描述
假如里面有多个属性,比如调用producer,class Phone里面也有,类NFC里面也有,此时谁比较靠左边就是谁

复写父类

在这里插入图片描述
我继承过来的属性和方法我都不满意,我就直接改

class Phone:id=Noneproducer="可是雪"def call_by_5g(self):print("5g通话")class MyPhone(Phone):producer = "不是雪"def call_by_5g(self):print("开启单核模式,确保省电")print("使用5g网络进行通话")print("关闭单核模式,确保性能")phone=MyPhone()
phone.call_by_5g()
print(phone.producer)

在这里插入图片描述
子类把父类覆盖掉
下面:
假如我已经把父类里面的东西复写掉了,但是我还想用父类的某些变量和方法,那么我可以使用下面两种方法
在这里插入图片描述

class Phone:id=Noneproducer="可是雪"def call_by_5g(self):print("5g通话")class MyPhone(Phone):producer = "不是雪"def call_by_5g(self):print("开启单核模式,确保省电")# print("使用5g网络进行通话")# 方式一print(f"父类的厂商是{Phone.producer}")Phone.call_by_5g(self)# 方式二print(f"父类的厂商是{super().producer}")super().call_by_5g()print("关闭单核模式,确保性能")phone=MyPhone()
phone.call_by_5g()
print(phone.producer)

应该比较喜欢super方法,方法一忘记传self还会报错

变量的类型注解

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

import json
import randomvar_1:int=10
var_2:str="hello"
var_3:bool=True
class Student:pass
stu:Student=Student()my_list:list[int]=[1,2,3,4]
my_tuple:tuple[int,str,bool]=(1,"itheima",True)
my_set:set[str,int]={"hahaha",4}
my_dict:dict={"a":1,"b":2}var_1=random.randint(1,10) #type:int
var_2=json.loads({"name":"zhangsan"})  #type:dict[str,str]

没逝,我以后大概率不干这行,这种备注感觉不进公司没啥用

函数和方法类型注解

在这里插入图片描述
在这里插入图片描述

# 对形参进行类型注解
def add(x:int, y:int):return x + y
# 对返回值进行类型注解
def func(data:list) ->list:return dataprint(func(1))
print(add(1,2))

在这里插入图片描述

Union联合类型注解

在这里插入图片描述
不是一一对应,只需要把后面出现的类型都列举出来就行

多态

在这里插入图片描述
在这里插入图片描述

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.speak()
# 不用注解
# def make_noise(Animal):
#     animal = Animal()
#     animal.speak()
dag=Dog()
cat=Cat()
make_noise(cat)
make_noise(dag)

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

class AC:def cool_wind(self):# 制冷passdef hot_wind(self):# 制热passdef swing_l_r(self):# 左右摆风passclass meidi(AC):def cool_wind(self):print("美的空调制冷")def hot_wind(self):print("美的空调制热")def swing_l_r(self):print("美的空调左右摆风")class geli(AC):def cool_wind(self):print("格力空调制冷")def hot_wind(self):print("格力空调制热")def swing_l_r(self):print("格力空调左右摆风")meidi = meidi()
meidi.cool_wind()
geli = geli()
geli.cool_wind()

在这里插入图片描述
类似于我给一个猜想,但是让其他人去做

数据分析综合案例

我有畏难心理,我先害怕了
果然,都明白但是自己就是不会敲,占个位
明儿在写

SQL

在这里插入图片描述

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com