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[属性名] = ...
随机推荐
- uni app canvas 不生效
canvas 创建canvas绘图上下文. <canvas style="width: 300px; height: 200px;" canvas-id="firs ...
- vnpy源码阅读学习(3):学习vnpy的界面的实现
学习vnpy的界面的实现 通过简单的学习了PyQt5的一些代码以后,我们基本上可以理解PyQt的一些用法,下面让我们来先研究下vnpy的UI部分的代码. 首先回到上一节看到的run.py(/vnpy/ ...
- postman传递当前时间戳
有时我们在请求接口时,需要带上当前时间戳这种动态参数,那么postman能不能自动的填充上呢. 1请求动态参数(例如时间戳) 直接在参数值写 {{$timestamp}} 如下: 我们也可以使用pos ...
- 从头学pytorch(十九):批量归一化batch normalization
批量归一化 论文地址:https://arxiv.org/abs/1502.03167 批量归一化基本上是现在模型的标配了. 说实在的,到今天我也没搞明白batch normalize能够使得模型训练 ...
- dp-最长递增子序列 (LIS)
首先引出一个例子 问题 : 给你一个长度为 6 的数组 , 数组元素为 { 1 ,4,5,6,2,3,8 } , 则其最长单调递增子序列为 { 1 , 4 , 5 , 6 , 8 } , 并且长度为 ...
- max count混合使用
SELECT MAX(a1.人数) FROM (SELECT COUNT(category_id) AS "人数",category_id FROM course_category ...
- Python判断一个字符串是否包含某个指定的字符串
成员操作符 in str = "string test string test" find1 = "str" find2 = "test" ...
- git 多人协同开发
一.允许他人操作程序(两种方式) 1.添加合作者 2.创建组织 二.分支 每个开发者创建一个属于他们的dev分支 三.合并规则 1.一起合并(代码会出现冲突) 2.尽量缩短合并的周期
- SqlServer分页存储过程(多表查询,多条件排序),Repeater控件呈现数据以及分页
存储过程(Stored Procedure)是在大型数据库系统中,一组为了完成特定功能的SQL 语句集,存储在数据库中,经过第一次编译后再次调用不需要再次编译,用户通过指定存储过程的名字并给出 ...
- 【强化学习RL】model-free的prediction和control —— MC,TD(λ),SARSA,Q-learning等
本系列强化学习内容来源自对David Silver课程的学习 课程链接http://www0.cs.ucl.ac.uk/staff/D.Silver/web/Teaching.html 本文介绍了在m ...