当前位置: 首页 > news >正文

Python:多态,静态方法和类方法

多继承的弊端:容易引发冲突,增加代码设计复杂度#****#

多态:

class Animal(object):
    def shoot(self):
        print("shoot")
class Cat(Animal):
    def shoot(self):
        print("momo")
class Dog(Animal):
    def shoot(self):
        print("wawa")
cat=Cat()
cat.shoot()
dog=Dog()
dog.shoot()

输出结果为:

momo
wawa
 

多态性:一种调用方式,能够有不同的执行结果:

class Animal(object):
    def shoot(self):
        print("shoot")
class Cat(Animal):
    def shoot(self):
        print("momo")
class Dog(Animal):
    def shoot(self):
        print("wawa")
#多态性:定义一个统一的接口,一个接口多种实现
def test(obj):#函数传入不同的对象执行不同对象的方法
    obj.shoot()
animal=Animal()
cat=Cat()
dog=Dog( )
test(animal)
test(cat)
test(dog)

输出结果为:
shoot
momo
wawa

静态方法:

使用语法糖装饰器@staticmethod来进行修饰,静态方法没有self,cls参数的限制

不需要访问实例属性和类属性,如果要访问类属性,通过类名,类属性名访问,不能访问实例属性

与类无关,可以转换成函数使用:
 

class Animal(object):
    @staticmethod
    def shoot(name):
        print(f"{name}shoot")
#静态方法既可以使用对象访问,也可以使用类访问
Animal.shoot('lihailu')#调用方法时传参数
pe=Animal()
pe.shoot('liahilu')

应用场景:取消不必要的参数传递有利于减少不必要的内存占用和性能消耗

类方法:使用装饰器@classmethod来标识为类方法,第一个参数必须是类对象,一般的话是以cls作为第一个参数:

class Animal(object):
    @classmethod
    def shoot(cls):
        print(cls)#cls代表类对象本身,类本质上就是一个对象
        print("shoot")
pe=Animal()
pe.shoot()

输出结果为:
<class '__main__.Animal'>
shoot

类方法内部可以访问类属性,或者调用其他的类方法,可以通过cls.类属性名访问类属性,不能访问实例属性:

class Animal(object):
    name='lihailu'
    @classmethod
    def shoot(cls):
        print(cls)
        print("shoot")
        print(cls.name)#Animal.name
pe=Animal()
pe.shoot()

应用场景:当方法中需要使用到类对象(如访问私有类属性等),定义类方法

类方法一般配合类属性使用

class Animal(object):
    name='lihailu'#类属性,对象拥有的
    def __init__(self):
        self.age=18;#实例属性,对象私有的
    def play(self):#实例方法
        #在实例方法中访问类属性
        print(f"{Animal.name} is playing")
pe=Animal()
pe.play()

class Animal(object):
    name='lihailu'#类属性,对象拥有的
    def __init__(self):
        self.age=18;#实例属性,对象私有的
    # def play(self):#实例方法
    #     #在实例方法中访问类属性
    #     print(f"{Animal.name} is playing")
    #     print(self.age)
    @staticmethod
    def introduce():
        print(f"I am {Animal.name}")#能访问到类属性但没意义
pe=Animal()
pe.introduce()

class Animal(object):
    name='lihailu'#类属性,对象拥有的
    def __init__(self):
        self.age=18;#实例属性,对象私有的
    # def play(self):#实例方法
    #     #在实例方法中访问类属性
    #     print(f"{Animal.name} is playing")
    #     print(self.age)
    @classmethod
    def introduce(cls):
        print(f"I am {cls.name}")#代表类对象本身
pe=Animal()
pe.introduce()

总结:类属性是公共的,所有方法都能够访问到,静态方法不需要访问类属性(静态方法和类与对象没有关联),实例属性是私有的,只有实例方法内部能够访问到

相关文章:

  • golang 生成单元测试报告
  • 目标检测——清洗数据
  • Java 填充 PDF 模版
  • Python个人学习笔记(18):模块(异常处理、traceback、日志记录)
  • MAC-在使用@Async注解的方法时,分布式锁管理和释放
  • STM32原理性知识
  • 一种基于大规模语言模型LLM的数据分析洞察生成方法
  • 如何在 Node.js 中使用 .env 文件管理环境变量 ?
  • Rust嵌入式开发环境搭建指南(基于Stm32+Vscode)
  • ASP3605同步降压调节器——满足汽车电子严苛要求的电源芯片方案
  • 数学之握手问题
  • Java替换jar包中class文件
  • Matlab概率区间预测全家桶更新了,新增光伏出力区间预测,4种分布可供预测
  • 【单片机通信技术应用——学习笔记三】液晶屏显示技术,取模软件的应用
  • AI重构工程设计、施工、总承包行业:从智能优化到数字孪生的产业革命
  • 【C++】八大常见的设计模式的实现与实践指南
  • Flink 内存管理
  • 3.18练习
  • 实现图片多种处理需求的实用工具
  • 功能安全实战系列06-英飞凌Tricore系列SMU详解
  • 影子调查丨起底“三无”拖拉机产销链:出口掩内销,监管如虚设
  • 谁将主导“视觉大脑”?中国AI的下一个超级赛道
  • 内蒙古纪检干部刘占波履新呼和浩特,曾参与涉煤腐败倒查20年工作
  • 李彦宏:DeepSeek不是万能,多模态将是未来基础模型的标配
  • 三亚亚龙湾3.4公里岸线近岸海域使用权挂牌出让,起始价近九千万
  • 习近平举行仪式欢迎肯尼亚总统鲁托访华