当前位置: 首页 > news >正文

从零开始的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

在这里插入图片描述

http://www.lryc.cn/news/399040.html

相关文章:

  • 【并发编程】进程 线程 协程
  • Vue的生命周期函数有哪些?详细说明
  • 大语言模型应用--AI工程化落地
  • 我会什么开发技能
  • Run LoongArch64 Alpine VM on x86_64
  • 4层负载均衡和7层负载均衡
  • 前端Vue组件化实践:打造仿京东天猫商品属性选择器组件
  • 智慧城市3d数据可视化系统提升信息汇报的时效和精准度
  • Git 详解(原理、使用)
  • android11为开机动画添加铃声(语音)
  • 使用 Akshare 下载国内的期货(主力连续)、股票和指数的历史行情数据
  • 【React】Google 账号之个性化一键登录按钮功能
  • MySQL已经连接对应数据库,但mapper中表名仍报错
  • CentOS 7:停止更新后如何下载软件?
  • MySQL GROUP_CONCAT 函数详解与实战应用
  • MATLAB Gazebo联合仿真
  • Vue3 pdf.js将二进制文件流转成pdf预览
  • 【机器学习】逻辑回归的原理、应用与扩展
  • Ubuntu22.04系统装好后左上角下划线闪烁不开机(N卡)
  • Leetcode刷题4--- 寻找两个正序数组的中位数 Python
  • springBoot(若依)集成camunda
  • 【微信小程序知识点】自定义构建npm
  • JCR一区 | Matlab实现GAF-PCNN-MATT、GASF-CNN、GADF-CNN的多特征输入数据分类预测/故障诊断
  • 新手教学系列——高效管理MongoDB数据:批量插入与更新的实战技巧
  • C# Winform 自定义事件实战
  • Python通过继承实现多线程
  • 记一次项目经历
  • Elasticsearch 8 支持别名查询
  • 【Spring Cloud】 使用Eureka实现服务注册与服务发现
  • JDK安装详细教程(以JDK17为例)