031.Python类中的方法
一 类中的方法
1.1 介绍
(1) 普通方法
(2) 绑定方法
- 绑定到对象 (自动传递对象参数)
- 绑定到类 (自动传递类参数)
(3) 静态方法 (无论类还是对象,都可以调用)
class Plane():
def __init__(self,name):
self.name = name
#绑定到对象
def fly(self):
print ("plane can fly")
#普通方法
def capitain():
print ("will have a capitain")
#绑定方法(绑定到类)
@classmethod
def save (cls):
print ("will help people")
#静态方法(对象,类都可以调用)
@staticmethod
def attack():
print("some bad people use if for attack")
obj = Plane() #普通方法的调用只能使用类来调用,因为没有参数
Palne.capitain()
执行
[root@node10 python]# python3 test.py
Traceback (most recent call last):
File "test.py", line 18, in <module>
obj = Plane()
TypeError: __init__() missing 1 required positional argument: 'name'
对象使用参数
class Plane():
def __init__(self,name):
self.name = name
#绑定到对象
def fly(self):
print ("plane can fly")
#普通方法
def capitain():
print ("will have a capitain")
#绑定方法(绑定到类)
@classmethod
def save (cls):
print ("will help people")
#静态方法(对象,类都可以调用)
@staticmethod
def attack():
print("some bad people use if for attack")
obj = Plane("benladeng") #普通方法的调用,只能使用类来调用,因为没有参数
Plane.capitain()
obj.capitain() #这里会默认传参,不能一一对应
执行
self 系统会默认传递,但是新参没有,不能一一对应
[root@node10 python]# python3 test.py
will have a capitain
Traceback (most recent call last):
File "test.py", line 22, in <module>
obj.capitain()
TypeError: capitain() takes 0 positional arguments but 1 was given
1.2 绑定方法的调用
[root@node10 python]# cat test.py
class Plane():
def __init__(self,name):
self.name = name
#绑定到对象
def fly(self):
print ("plane can fly")
#普通方法
def capitain():
print ("will have a capitain")
#绑定方法(绑定到类)
@classmethod
def save (cls):
print ("will help people")
#静态方法(对象,类都可以调用)
@staticmethod
def attack():
print("some bad people use if for attack")
obj = Plane("benladeng") #绑定方法的调用
obj.fly()
执行
[root@node10 python]# python3 test.py
plane can fly
由于方法fly只是简单的打印,就可以使用对象调用
class Plane():
def __init__(self,name):
self.name = name
#绑定到对象
def fly(self):
print ("plane can fly")
#普通方法
def capitain():
print ("will have a capitain")
#绑定方法(绑定到类)
@classmethod
def save (cls):
print ("will help people")
#静态方法(对象,类都可以调用)
@staticmethod
def attack():
print("some bad people use if for attack")
obj = Plane("benladeng") #绑定方法的调用
obj.fly()
Plane.fly(111)
执行
[root@node10 python]# python3 test.py
plane can fly
plane can fly
当带有self的参数就会报错(如果函数体里用到了该对象,类名调用的方式不可以.)
class Plane():
def __init__(self,name):
self.name = name
#绑定到对象
def fly(self):
#print ("plane can fly")
print ("plane can fly",self.name)
#普通方法
def capitain():
print ("will have a capitain")
#绑定方法(绑定到类)
@classmethod
def save (cls):
print ("will help people")
#静态方法(对象,类都可以调用)
@staticmethod
def attack():
print("some bad people use if for attack")
obj = Plane("benladeng") #绑定方法的调用
Plane.fly(111)
执行
[root@node10 python]# python3 test.py
Traceback (most recent call last):
File "test.py", line 22, in <module>
Plane.fly(111)
File "test.py", line 7, in fly
print ("plane can fly",self.name)
AttributeError: 'int' object has no attribute 'name'
1.3 绑定到类方法的调用
class Plane():
def __init__(self,name):
self.name = name
#绑定到对象
def fly(self):
#print ("plane can fly")
print ("plane can fly",self.name)
#普通方法
def capitain():
print ("will have a capitain")
#绑定方法(绑定到类)
@classmethod
def save (cls):
print ("will help people")
#静态方法(对象,类都可以调用)
@staticmethod
def attack():
print("some bad people use if for attack")
obj = Plane("benladeng") #绑定到类方法的调用
Plane.save()
执行
[root@node10 python]# python3 test.py
will help people
打印出这个类
class Plane():
def __init__(self,name):
self.name = name
#绑定到对象
def fly(self):
#print ("plane can fly")
print ("plane can fly",self.name)
#普通方法
def capitain():
print ("will have a capitain")
#绑定方法(绑定到类)
@classmethod
def save (cls):
print (cls)
print ("will help people")
#静态方法(对象,类都可以调用)
@staticmethod
def attack():
print("some bad people use if for attack")
obj = Plane("benladeng") #绑定到类方法的调用
Plane.save()
执行
[root@node10 python]# python3 test.py
<class '__main__.Plane'>
will help people
对象可以调用类中的属性和方法,但是类不能调用对象中的属性和方法
class Plane():
def __init__(self,name):
self.name = name
#绑定到对象
def fly(self):
#print ("plane can fly")
print ("plane can fly",self.name)
#普通方法
def capitain():
print ("will have a capitain")
#绑定方法(绑定到类)
@classmethod
def save (cls):
cls.name
print (cls)
print ("will help people")
#静态方法(对象,类都可以调用)
@staticmethod
def attack():
print("some bad people use if for attack")
obj = Plane("benladeng") #绑定到类方法的调用
Plane.save()
执行
[root@node10 python]# python3 test.py
Traceback (most recent call last):
File "test.py", line 24, in <module>
Plane.save()
File "test.py", line 14, in save
cls.name
AttributeError: type object 'Plane' has no attribute 'name'
1.4 调用类中的属性
class Plane():
material = "合金"
def __init__(self,name):
self.name = name
#绑定到对象
def fly(self):
#print ("plane can fly")
print ("plane can fly",self.name)
#普通方法
def capitain():
print ("will have a capitain")
#绑定方法(绑定到类)
@classmethod
def save (cls):
print (cls.material)
print ("will help people")
#静态方法(对象,类都可以调用)
@staticmethod
def attack():
print("some bad people use if for attack")
obj = Plane("benladeng") #绑定到类方法的调用
Plane.save()
执行
[root@node10 python]# python3 test.py
合金
will help people
1.5 对象调用
先把obj所归属的类找出来,然后把该类当成参数进行传递.
class Plane():
material = "合金"
def __init__(self,name):
self.name = name
#绑定到对象
def fly(self):
#print ("plane can fly")
print ("plane can fly",self.name)
#普通方法
def capitain():
print ("will have a capitain")
#绑定方法(绑定到类)
@classmethod
def save (cls):
print (cls.material)
print ("will help people")
#静态方法(对象,类都可以调用)
@staticmethod
def attack():
print("some bad people use if for attack")
obj = Plane("benladeng") #绑定到类方法的调用
Plane.save()
obj.save()
执行
[root@node10 python]# python3 test.py
合金
will help people
合金
will help people
1.6 静态方法的调用
class Plane():
material = "合金"
def __init__(self,name):
self.name = name
#绑定到对象
def fly(self):
#print ("plane can fly")
print ("plane can fly",self.name)
#普通方法
def capitain():
print ("will have a capitain")
#绑定方法(绑定到类)
@classmethod
def save (cls):
print (cls.material)
print ("will help people")
#静态方法(对象,类都可以调用)
@staticmethod
def attack():
print("some bad people use if for attack")
obj = Plane("benladeng")
#静态方法的调用
obj.attack()
Plane.attack()
执行
[root@node10 python]# python3 test.py
some bad people use if for attack
some bad people use if for attack
调用成功
031.Python类中的方法的更多相关文章
- python 类中的方法
首先,方法是类内部定义的函数,所以方法是类的属性而不是实例的属性. 其次,方法只能在所属的类拥有实例的时候才能被调用.当存在一个实例的时候,我们可以说方法被绑定到实例.如果没有实例,那么我们就说方法是 ...
- Python 类中__init__()方法中的形参与如何修改类中属性的值
一.__init__()方法 如果__init__()方法为 class Cat(): def __init__(self,num) : self.num=num Python中类的__init__( ...
- Python 简明教程 --- 20,Python 类中的属性与方法
微信公众号:码农充电站pro 个人主页:https://codeshellme.github.io 与客户保持良好的关系可以使生产率加倍. -- Larry Bernstain 目录 类中的变量称为属 ...
- Python 装饰器装饰类中的方法
title: Python 装饰器装饰类中的方法 comments: true date: 2017-04-17 20:44:31 tags: ['Python', 'Decorate'] categ ...
- 如何访问python类中的私有方法
在python中,不像c#/java类语言,支持类的私有方法,这点有点像objc,虽然objc可以通过扩展extension来实现,但源于objc的运行时特性,我们还是可以通过非常手段来进行访问的.不 ...
- 孤荷凌寒自学python第二十四天python类中隐藏的私有方法探秘
孤荷凌寒自学python第二十四天python类中隐藏的私有方法探秘 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) 今天发现了python的类中隐藏着一些特殊的私有方法. 这些私有方法不管我 ...
- python 类中方法总结 --- 实例方法、类方法、静态方法
在python的类语法中,可以出现三种方法,具体如下: (1)实例方法 1)第一个参数必须是实例本身,一般使用[self]表示. 2)在实例方法中,可以通过[self]来操作实例属性,[类名]来操作类 ...
- 第8.6节 Python类中的__new__方法深入剖析:调用父类__new__方法参数的困惑
上节<第8.5节 Python类中的__new__方法和构造方法__init__关系深入剖析:执行顺序及参数关系案例详解>通过案例详细分析了两个方法的执行顺序,不知大家是否注意到了,在上述 ...
- 第8.12节 Python类中使用__dict__定义实例变量和方法
上节介绍了使用实例的__dict__查看实例的自定义属性,其实还可以直接使用__dict__定义实例变量和实例方法. 一. 使用__dict__定义实例变量 语法: 对象名. dict[属性名] = ...
随机推荐
- 用python搭一个超简易的文件服务器
这个文件服务器纯粹是在学习python cgi编程时,顺便玩玩而已,因为搭文件服务器的话完全可以linux,简单方便,这里就是随便玩玩,功能也就是只能下载文件 1.登录页面,做个简单验证 新建一个ht ...
- 七种武器:JavaScript 新特性闪亮登场
JavaScript(或ECMA Script) 是一门不断发展的语言,有许多关于如何前进的建议和想法.TC39(技术委员会39)是负责定义JS标准和特性的委员会,今年他们非常活跃.以下是目前处于&q ...
- Beetlex实现完整的HTTP协议
在传统网络服务中扩展中需要处理Bytes来进行协议的读写,这种原始的处理方式让工作变得相当繁琐复杂,出错和调试的工作量都非常大:组件为了解决这一问题引用Stream读写方式,这种方式可以极大的简化网络 ...
- Logback源码分析
在日常开发中经常通过打印日志记录程序执行的步骤或者排查问题,如下代码类似很多,但是,它是如何执行的呢? package chapters; import org.slf4j.Logger; impor ...
- python 进程事件
1.作用 通过信号量,控制全部进程进入阻塞状态,也可以通过控制信号量,解除全部进程的阻塞 注意:定义的事件对象,默认状态是阻塞 2.常用方法 """ 对象.set() 作 ...
- influxdb的命令们
InfluxDB是一个开源的时序数据库,使用GO语言开发,特别适合用于处理和分析资源监控数据这种时序相关数据.而InfluxDB自带的各种特殊函数如求标准差,随机取样数据,统计数据变化比等,使数据统计 ...
- 玩转Django2.0---Django笔记建站基础十一(一)(音乐网站开发)
第十一章 音乐网站开发 本章以音乐网站项目为例,介绍Django在实际项目开发中的应用,该网站共分为6个功能模块分别是:网站首页.歌曲排行榜.歌曲播放.歌曲点评.歌曲搜索和用户管理. 11.1 网站需 ...
- mysql安装教程linux
https://www.cnblogs.com/YangshengQuan/p/8431520.html 设置sql远程访问
- Electron使用electron-packager打包记录
1.使用 JavaScript, HTML 和 CSS 构建跨平台的桌面应用 2.下载https://github.com/electron/electron-quick-start中的示例 3.在示 ...
- 论文翻译:Mastering the Game of Go without Human Knowledge (第一部分)
长久以来,人工智能的一个目标是在那些具有挑战性的领域实现超过人类表现的算法.最近,AlphaGo成为了在围棋上第一个打败了世界冠军的程序.在AlphaGo中,使用深度神经网络来进行树搜索,评估位置,和 ...