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[属性名] = ...
随机推荐
- CF825G Tree Queries
[题意] 一棵树有 n个节点,初始均为白色,有两种操作: 1. 1 x 代表把结点 x 设置为黑色 2. 2 x 代表查询 x 到树上任意一个黑色结点的简单路径上的编号最小的结点的编号 输入 t 和 ...
- Spring学习记录3——Spring AOP
SpringAOP基础 AOP简介: AOP是Aspect Oriented Programing的简称,翻译为“面向切面编程”.它适用于具有横切逻辑的应用场合,如性能检测,访问控制,事务管理及日志记 ...
- JavaScript(1)---绑定事件、解除绑定事件
JavaScript(1)---绑定事件.解除绑定事件 一.事件概述 1.事件的几个概念 · 事件 指的是文档或者浏览器窗口中发生的一些特定交互瞬间.我们可以通过侦听器(或者处理程序)来预定事件,以便 ...
- 20191102Java课堂记录
1. import javax.swing.*; class AboutException { public static void main(String[] a) { int i=1, j=0, ...
- 字典 pop
1.pop(key) 删除键值对,返回value2.若字典中没有这个key,则返回None,也可以自定义3.可用作if条件判断 来源: rest framework 框架 Serializer que ...
- JS 点击验证码刷新
<img src="/get_valid_img" id="valid-img" title="点击再换一张" class=" ...
- .NET Core验证ASP.NET密码
.NET Core验证ASP.NET密码 随着.NET Core的持续更新和完善,越来越多的机构已经选择或者升级为.NET Core.但由于技术不完全相同,不可能所有应用/数据库都能无缝迁移,因此AS ...
- python类型-序列-列表
列表类型也是序列式的数据类型,可通过下标或者切片操作来访问某一个或某一块连续的元素. 列表的元素是可变的,可包含不同类型的元素,列表类型的元素可以是另一个序列类型. 1.创建列表类型数据并赋值 列表使 ...
- 「雅礼集训 2017 Day2」棋盘游戏
祝各位圣诞后快乐(逃) 题目传送门 分析: 首先棋盘上的路径构成的图是一张二分图 那么对于一个二分图,先求出最大匹配,先手如果走到关键匹配点,只要后手顺着匹配边走,由于不再会出现增广路径,所以走到最后 ...
- AVLtree(C++实现)有统一的旋转操作
在学习完AVLtree之后,我发现,左旋,右旋均可以采用统一的旋转方式来实现,所以把代码贴在下面 代码是完整的AVLTree实现 C++标准为C++11 在ubuntu 18.04下通过编译和调试 / ...