zoukankan      html  css  js  c++  java
  • 031.Python类中的方法


    一 类中的方法

    1.1 介绍

    (1) 普通方法
    (2) 绑定方法

    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

    调用成功

    学习记录,小白一枚
  • 相关阅读:
    深入理解分布式事务,高并发下分布式事务的解决方案
    Java互联网架构-Mysql分库分表订单生成系统实战分析
    PowerDesigner 表格导出为excel
    并行(多进程)-python
    使用caffe模型测试图片(python接口)
    评估模型-二分类
    python-字符编码
    python-随机操作(random)
    目标检测-yolo2
    tensorflow-安装
  • 原文地址:https://www.cnblogs.com/wangsirde0428/p/14322590.html
Copyright © 2011-2022 走看看