面向对象(五)

0. 方法和函数


  • 区别
    1. 概念
       (1) 方法:描述一个目标的 行为动作
       (2) 函数:一段具有 特定功能 的代码块
    
    2. 调用方式
       (1) 方法:通过对象来调用
       (2) 函数:通过函数名来调用
    
    3. 数据共享
       (1) 方法之间可以共享数据
       (2) 函数都是独立的个体, 函数之间几乎没有共享的数据
    
  • 相同点
    1. 都封装了一系列行为动作
    2. 都可以被调用之后,执行一系列行为动作
    
  • 判定依据:是否存在 归属对象

1. 实例方法


  • 概念:默认 第一个参数 需要接收到一个 实例对象
    1. 语法:
       class 类名:
           def 实例方法名(self):
               代码
    2. 调用
       (1) 使用实例对象调用, 不用手动传入第一个参数, 解释器会自动把调用实例对象本身传递过去
           class Person:
               def play(self, ball):
                   print("在打球", self, ball)
           p = Person()
           print(p)
           p.play("篮球")
    
       (2) 使用类对象调用, 本质就是直接找到函数本身来调用, 一般不使用
           class Person:
               def play(self, ball):
                   print("在打球", self, ball)
           # 找到函数实现地址,直接调用
           Person.play("luck", "you") 
    
       (3) 间接调用, 本质就是直接找到函数本身来调用, 一般不使用
           class Person:
               def play(self, ball):
                   print("在打球", self, ball)
           # 找到函数实现地址
           func = Person.play
           # 调用
           func("luck", "you")
    
    3. 注意
       (1) 实例方法第一个形参名称可以不是 self , 但是默认会用 self
           class Person:
               def play(name, ball):
                   print("在打球", name, ball)
           p = Person()
           print(p)
           p.play("篮球")
        
       (2) 如果实例方法没有接收任何参数, 则会报错
           class Person:
               def play():
                   print("在打球")
           p = Person()
           p.play()
    
  • 底层简述
    底层简述.png

2. 类方法


  • 概念:默认 第一个参数 需要接收到一个 类对象
    1. 语法
       class 类名:
           @classmethod
           def 类方法名(cls, score):
               代码
    
    2. 调用
       (1) 使用类对象调用, 不用手动传入第一个参数, 解释器会自动把调用类对象本身传递过去
           class Person:
               @classmethod
               def run(cls, score):
                   print("在跑步", cls, score)
           print(Person)
           Person.run(100)
      
       (2) 使用实例对象调用, 不用手动传入第一个参数, 解释器会自动把调用实例对象对应的类对象
           给传递过去, 一般不使用
           class Person:
               @classmethod
               def run(cls, score):
                   print("在跑步", cls, score)
           p = Person()
           print(p.__class__)
           p.run(150)
    
       (3) 间接调用, 本质就是直接找到函数本身来调用, 一般不使用
           class Person:
               @classmethod
               def run(cls, score):
                   print("在跑步", cls, score)
           func = Person.run
           print(Person)
           func(300)
    
    3. 注意
       (1) 类方法第一个形参名称可以不是 cls , 但是默认会用 cls
           class Person:
               @classmethod
               def run(name, score):
                   print("在跑步", name, score)
           print(Person)
           Person.run(500)
    
       (2) 如果类方法没有接收任何参数, 则会报错
           class Person:
               @classmethod
               def run():
                   print("在跑步")          
           print(Person)
           Person.run()
    
  • 底层简述
    底层简述.png

3. 静态方法


  • 概念:如果有 第一个参数, 不会默认接收 任何对象
    1. 语法
       class 类名:
           @staticmethod
           def 静态方法名():
               代码
    
    2. 调用
       (1) 使用实例对象调用
           class Person:
               @staticmethod
               def fly(height):
                   print("飞起来了", height)
           p = Person()
           p.fly(100)
    
       (2) 使用类对象调用
           class Person:
               @staticmethod
               def fly(height):
                   print("飞起来了", height)
           Person.fly(150)
    
       (3) 间接调用, 本质就是直接找到函数本身来调用, 一般不使用
           class Person:
               @staticmethod
               def fly(height):
                   print("飞起来了", height)
           func = Person.fly
           func(300)
    
  • 底层简述
    底层简述.png

4. 方法存储


  • 概念:实例方法类方法静态方法 都存储在 类对象__dict__ 属性中
    class Person:
        def play(self, ball):
            print("在打球", self, ball)
        @classmethod
        def run(cls, score):
            print("在跑步", cls, score)
        @staticmethod
        def fly(height):
            print("飞起来了", height)
    print(Person.__dict__)
    
  • 底层简述
    底层简述.png

5. 属性访问


  • 实例方法
    1. 可以访问对象属性
    2. 可以访问类属性
       class Person:
           age = 18
           def play(self):
               print(self.age)
               print(self.num)
       p = Person()
       p.num = 10
       p.play()
    
  • 类方法
    1. 可以访问类属性
       class Person:
           age = 18
           @classmethod
           def run(cls):
               print(cls.age)
       Person.run()
    
  • 静态方法
    1. 可以访问类属性
       class Person:
           age = 18
           @staticmethod
           def fly():
               print(Person.age)
       Person.fly()
    

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容