满足低调之心基础七(1)
一,静态方法,类方法,属性方法
1,静态方法
咱们先任意写一个类:
class Dog(object):def __init__(self, name, food):self.NAME = nameself.FOOD = fooddef eat(self):print("The dog \033[31;1m%s\033[0m is eating \033[34;1m%s\033[0m" % (self.NAME, self.FOOD))d = Dog("huahua", "meat")
d.eat()G:\Python38\python.exe G:/Project1/self_taught/week_7/static_method.py
The dog huahua is eating meatProcess finished with exit code 0
这个已经很熟悉了,那么再看
class Dog(object):def __init__(self, name, food):self.NAME = nameself.FOOD = food@staticmethoddef eat(self):print("The dog \033[31;1m%s\033[0m is eating \033[34;1m%s\033[0m" % (self.NAME, self.FOOD))d = Dog("huahua", "meat")
d.eat()
当我们加了个东西呢?运行发现报错了!
G:\Python38\python.exe G:/Project1/self_taught/week_7/static_method.py
Traceback (most recent call last):File "G:/Project1/self_taught/week_7/static_method.py", line 31, in <module>d.eat()
TypeError: eat() missing 1 required positional argument: 'self'Process finished with exit code 1
说呀,eat()少一个参数"self",纳尼,是不是丈二和尚 —— 摸不着头脑
其实呢,加上了@staticmethod实际上eat()就和类没有什么关系了,只是类下面的一个函数而已。
那我就要获取eat()里的东西呢?
class Dog(object):def __init__(self, name, food):self.NAME = nameself.FOOD = food@staticmethoddef eat():print("The dog \033[31;1m%s\033[0m is eating \033[34;1m%s\033[0m" % ("huanhuan", "fish"))d = Dog("huahua", "meat")
d.eat()G:\Python38\python.exe G:/Project1/self_taught/week_7/static_method.py
The dog huanhuan is eating fishProcess finished with exit code 0
只需要借类名来调用下。
静态方法:只是名义上归类管,实际上已经访问不了类或实例中的任何属性。
2,类方法
在eat()上面加上@classmethod
class Dog(object):def __init__(self, name, food):self.NAME = nameself.FOOD = food@classmethoddef eat(self):print("The dog \033[31;1m%s\033[0m is eating \033[35;1m%s\033[0m" % (self.NAME, "fish"))d = Dog("huahua", "meat")
d.eat()
发现运行后也报错了,报错的最后一句为:AttributeError: type object ‘Dog’ has no attribute ‘NAME’, 说Dog类没有"NAME"属性
再改改:
class Dog(object):n = "Tom" # 类变量def __init__(self, name, food):self.NAME = nameself.FOOD = food@classmethoddef eat(self):print("The dog \033[31;1m%s\033[0m is eating \033[35;1m%s\033[0m" % (self.n, "fish"))d = Dog("huahua", "meat")
d.eat()G:\Python38\python.exe G:/Project1/self_taught/week_7/class_method.py
The dog Tom is eating fishProcess finished with exit code 0
这就是类方法:只能访问类变量,不能访问实例属性
3,属性方法
class Dog(object):def __init__(self, name):self.NAME = name@propertydef eat(self):print("The dog \033[31;1m%s\033[0m is eating \033[34;1m%s\033[0m" % (self.NAME, "fish"))d = Dog("huahua")
d.eat()
运行时会报错,报错内容为:TypeError: ‘NoneType’ object is not callable,说对象不可调用。
那么尝试改变一下
class Dog(object):def __init__(self, name):self.NAME = name@propertydef eat(self):print("The dog \033[31;1m%s\033[0m is eating \033[34;1m%s\033[0m" % (self.NAME, "fish"))d = Dog("huahua")
d.eat # ()去掉相当于调变量一样G:\Python38\python.exe G:/Project1/self_taught/week_7/attribute_method.py
The dog huahua is eating fishProcess finished with exit code 0
so,属性方法:把一个方法变成一个静态属性
没有括号的话就没法传参数了,不过好像可以赋值,但是不能直接的赋值
class Dog(object):def __init__(self, name):self.NAME = name@propertydef eat(self):print("The dog \033[31;1m%s\033[0m is eating \033[34;1m%s\033[0m" % (self.NAME, "fish"))@eat.setterdef eat(self, sleep):print("\033[36;1m%s\033[0m is sleeping..." % sleep)d = Dog("huahua")
d.eat
d.eat = "pig"G:\Python38\python.exe G:/Project1/self_taught/week_7/attribute_method.py
The dog huahua is eating fish
pig is sleeping...Process finished with exit code 0
假如我想删除呢?
class Dog(object):def __init__(self, name):self.NAME = name@propertydef eat(self):print("The dog \033[31;1m%s\033[0m is eating \033[34;1m%s\033[0m" % (self.NAME, "fish"))@eat.setterdef eat(self, sleep):print("\033[36;1m%s\033[0m is sleeping..." % sleep)d = Dog("huahua")
d.eat
d.eat = "pig"
del d.eat
运行结果会报错,报错内容为:AttributeError: can’t delete attribute,说不能删除属性
假如我就想删掉呢?
class Dog(object):def __init__(self, name):self.NAME = name@propertydef eat(self):print("The dog \033[31;1m%s\033[0m is eating \033[34;1m%s\033[0m" % (self.NAME, "fish"))@eat.setterdef eat(self, sleep):print("%s \033[36;1m%s\033[0m is sleeping..." % (self.NAME, sleep))@eat.deleterdef eat(self):del self.NAMEprint("deleted")d = Dog("huahua")
d.eat
d.eat = "pig"
del d.eatG:\Python38\python.exe G:/Project1/self_taught/week_7/attribute_method.py
The dog huahua is eating fish
huahua pig is sleeping...
deletedProcess finished with exit code 0
然后我们再查看
class Dog(object):def __init__(self, name):self.NAME = name@propertydef eat(self):print("The dog \033[31;1m%s\033[0m is eating \033[34;1m%s\033[0m" % (self.NAME, "fish"))@eat.setterdef eat(self, sleep):print("%s \033[36;1m%s\033[0m is sleeping..." % (self.NAME, sleep))@eat.deleterdef eat(self):del self.NAMEprint("deleted")d = Dog("huahua")
d.eat
d.eat = "pig"
del d.eat
d.eat
运行报错,报错内容为:AttributeError: ‘Dog’ object has no attribute ‘NAME’, 说Dog类没有"NAME"这个属性,说明删除成功!
满足低调之心基础七(1)相关推荐
- 满足低调之心基础十六
一些css基础内容补充
- C++学习基础七——深复制与浅复制
C++学习基础七--深复制与浅复制 一.深复制与浅复制基本知识 深复制和浅复制,又称为深拷贝和浅拷贝. 深复制和浅复制的区别如下图1所示: 图1 图1表示的是,定义一个类CDemo,包含int a和c ...
- JAVA基础七 类和对象
文章目录 JAVA基础七 类和对象 01 引用 02 继承 03 方法重载 04 构造方法 05 this 06 传参 07 包 08 访问修饰符 09 类属性 10 类方法 11 属性初始化 12 ...
- 视频教程-PHP零基础七天入门视频课程(免费50章)-PHP
PHP零基础七天入门视频课程(免费50章) 04年进入计算机行业.拥有6年net和php项目开发经验,8年java项目开发经验. 现前端全栈工程师,主攻产品设计,微信开发等. 黄菊华 ¥159.00 ...
- Pascal基础(七)-Pascal泛型
Pascal基础(七)-Pascal泛型 前言 目前很多语言都有泛型或者类似实现 C语言比较古老,没有泛型实现 C++ 有template来实现,个人对c++不懂,应该template和泛型不一样 j ...
- Cocos Shader入门基础七:一文彻底读懂深度图。
开篇 经过一段时间的持续输出,社区中越来越多的人踏上了3D图形渲染学习之旅,麒麟子非常开心,说明输出的内容对大家都产生了实际的帮助. 特别是上一篇 <用实时反射Shader增强画面颜值> ...
- javaWeb基础七天内容笔记汇总
今日内容: 1. web相关概念回顾 2. web服务器软件:Tomcat 3. Servlet入门学习 web相关概念回顾 软件架构 C/S:客户端/服务器端 B/S:浏览器/服务器端 资源分类 静 ...
- java基础(七) 深入解析java四种访问权限
戳上面的蓝字关注我们哦! 精彩内容 精选java等全套视频教程 精选java电子图书 大数据视频教程精选 java项目练习精选 引言 Java中的访问权限理解起来不难,但完全掌握却不容易,特别是4 ...
- oracle jdbc jar包_Oracle总结之plsql编程(基础七)
紧接基础六,对oracle角色和权限的管理之后,在接下来的几次总结中来就最近工作中用过的plsql编程方面的知识进行总结,和大家分享! 一.plsql块 1.只包括执行部分的plsql块 打开输出选项 ...
最新文章
- windows pxe 安装linux,菜鸟学Linux 第103篇笔记 pxe自动化安装linux
- mxnet保存模型,加载模型来预测新数据
- 项目:部署LNMP动态网站
- 关于排序速度效率数组集合选择那点事
- 搜狗浏览器缓冲区溢出漏洞EXP
- 腾讯为什么不开发linux软件下载,你认为国产操作系统如何搭建生态?为什么腾讯不给Linux系统适配QQ?...
- 在Bootstrap框架中,form-control的效果
- Sun公司网站上的Swing实例,想学Swing的不看后悔
- pdoModel封装
- php格式转为jpg格式,如何在PHP中将所有图像转换为JPG格式?
- pyinstaller打包tensorflow+python程序成.exe各种坑(持续添加)
- 十大排序思维导图(个人理解)
- Android阿里云推送离线通知集成踩坑之路
- CAD-Cass小结(6)——dat数据格式与展点(显示属性,更改点符号)
- 一份简单、直接、高效的中文求职信模板,一般是直接写在邮件正文中。
- 内核相关资源
开源/文档/社区/信息资源
1 http://www.kernel.org
Linux...
- HashMap常见面试题
- mysql 增量 命令
- 用python爬取今日头条上的图片_Python爬虫:抓取今日头条图集
- 【聚类算法】用Sklearn封装的KMeans | DBSCAN算法详解 |【问题解决】AttributeError: ‘NoneType‘ object has no attribute split
热门文章
- 【Java】2.Java体系架构(SE的组成概念图)
- View控件获得焦点,TextView获得焦点(focusable),自定义TextView使得其获得焦点,View的onFocusChange()
- 学妹问我没有实际项目经验,简历要怎么写?
- 【论文阅读】Phase-aware speech enhancement with deep complex U-net
- C# 将PPT转为OFD、DPT、DPS、ODP、POTX、UOP
- 多元线性回归之预测房价
- 大型传统企业的数字化创新之路
- 快速调出multisim里单刀双置开关
- react---收藏的点击和取消(刷新还会存在)--demo
- 微信公众平台测试帐号申请最新地址