031.Python类中的方法
梦中泪 人气:0一 类中的方法
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
调用成功
加载全部内容