python对象的三大特性介绍:封装/多态/继承

本文详细介绍了Python中类的封装、多态和继承三大特性,包括私有属性的使用、多态在子类重写父类方法的应用、继承中super函数的调用以及多重继承的示例和注意事项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

学习目录

1 类的封装

2 类的多态

3 类的继承

3.1 super函数调用父类的构造方法

3.2 重写父类方法

3.3 多重继承

之前文章我们介绍了类class的一些基本概念和使用,比如静态方法/类方法/@property装饰器等,但是提起编程中的对象,不得不学习下对象的三个特性:封装/多态和继承。

1 类的封装

封装是指将类中的属性或者方法在类中内部实现,不对外暴露,外界只能通过接口调用,而不能修改类中的内部实现。比如在类中定义一个方法返回内部的一些私有属性值。

举例:定义Car类,定义三个私有属性(私有属性以双下划线开头‘__’),封装一个方法print_property用来打印定义的私有属性。

class Car():
  def __init__(self,brand,color,cost_performance):
    self.__brand = brand
    self.__color = color
    self.__cost_performance = cost_performance
  def print_property(self):
    print(f'汽车品牌:{self.__brand} 颜色:{self.__color } 性价比:{self.__cost_performance}')

类实例化后,执行print_property方法。

Mercedes_Benz = Car('GLC260','black','不高')
Mercedes_Benz.print_property()
结果打印:
汽车品牌:GLC260 颜色:black 性价比:不高

当我们直接访问下私有属性时,会报错:

Mercedes_Benz.__brand
#打印结果:
AttributeError: 'Car' object has no attribute '__brand'

私有属性可以通过“实例名._类名__私有属性”来访问,如下

print(Mercedes_Benz._Car__brand)

2 类的多态

多态指的是同一个对象在不同情况下具有不同的表现形式和功能。一般使用在子类继承父类的时候,重写父类方法,虽然调用了相同方法,但实现了不同的功能。

举例:定义Car类,SuvCar子类和NsuvCar子类继承Car类,子类重写print_property方法。

class Car():
  def __init__(self,brand,color,cost_performance):
    self.__brand = brand
    self.__color = color
    self.__cost_performance = cost_performance
  def print_property(self):
    print(f'汽车品牌:{self.__brand} 颜色:{self.__color } 性价比:{self.__cost_performance}')

class SuvCar(Car):
  def __init__(self,brand,color,cost_performance):
    super().__init__(brand,color,cost_performance)
  def print_property(self):
    print(f'{self._Car__brand}汽车是SUV')

class NsuvCar(Car):
  def __init__(self,brand,color,cost_performance):
    super().__init__(brand,color,cost_performance)
  def print_property(self):
    print(f'{self._Car__brand}汽车不是SUV')

子类分别进行实例化,并调用print__property方法,发现虽然调用的是相同的方法,但是实现的功能确不相同。

suv = SuvCar('GLC260','black','low')
suv.print_property()
nsuv = NsuvCar('C200','white','high')
nsuv.print_property()
结果:
GLC260汽车是SUV
C200汽车不是SUV

3 类的继承

我们定义新的类称为子类(Subclass),而被继承的类称为基类或父类。子类可自动获得父类的全部变量和方法,省去了重复劳动,同时又可以对父类方法或者属性进行重写,或者追加新的属性或者方法。其语法结构如下:

class xxxx(A, B,C....):

      pass

Python支持单继承或者多继承。子类在调用某个方法或属性时,会优先在自己内部查找,如果没有找到,则开始根据继承机制在父类里查找。如果有多个父类,则根据特定的搜索顺序方式逐一查找父类

super函数调用父类的构造方法

继承时,有时我们需要直接调用父类的构造方法,几种方法如下都可使用

  • 类名.__init__(self, args)
  • super().__init__(args)
  • super(子类名,子类的实例).__init__(args)

举例:定义Car类,SuvCar子类/BusCar子类/ECar类都继承Car类,子类分别通过上述说明的三种方式的super函数调用父类的构造方法。

class Car():
  def __init__(self,brand,color,cost_performance):
    print('我是父类')
    self.__brand = brand
    self.__color = color
    self.__cost_performance = cost_performance

class SuvCar(Car):
  def __init__(self,brand,color,cost_performance):
    print('我是SuvCar类')
    Car.__init__(self,brand,color,cost_performance)

class BusCar(Car):
  def __init__(self,brand,color,cost_performance):
    print('我是BusCar类')
    super().__init__(brand,color,cost_performance)

class ECar(Car):
  def __init__(self,brand,color,cost_performance):
    print('我是ECar类')
    super(ECar,self).__init__(brand,color,cost_performance)

类实例化后,结果显示都调用了父类的初始化方法。

SuvCar('宋','black','high')
BusCar('yutong','black','high')
ECar('宋','black','high')
#打印结果:
我是SuvCar类
我是父类
我是BusCar类
我是父类
我是ECar类
我是父类

重写父类方法

举例:定义Car类,SuvCar子类继承Car类,子类重写print_property函数

class Car():
  def __init__(self,brand,color,cost_performance):
    print('我是父类')
    self.__brand = brand
    self.__color = color
    self.__cost_performance = cost_performance

class SuvCar(Car):
  def __init__(self,brand,color,cost_performance):
    print('我是SuvCar类')
    Car.__init__(self,brand,color,cost_performance)
  def print_property(self):
    print(f'{self._Car__brand}汽车是SUV')

类实例化后调用print_property方法,结果显示调用的是子类重写的方法。

Suv = SuvCar('宋','black','high')
Suv.print_property()
打印结果如下:
我是SuvCar类
我是父类
宋汽车是SUV

多重继承

多重继承指的是继承多个父类,举例:定义ECar子类,继承父类BusCar,SuvCar。

class Car():
  def __init__(self,brand,color,cost_performance):
    print('我是父类')
    self.__brand = brand
    self.__color = color
    self.__cost_performance = cost_performance

class SuvCar(Car):
  def __init__(self,brand,color,cost_performance):
    print('我是SuvCar类')
    Car.__init__(self,brand,color,cost_performance)
  def print_property(self):
    print(f'{self._Car__brand}汽车是SUV')

class BusCar(Car):
  def __init__(self,brand,color,cost_performance):
    print('我是BusCar类')
    super().__init__(brand,color,cost_performance)

class ECar(BusCar,SuvCar):
  def __init__(self,brand,color,cost_performance):
    print('我是ECar类')
    super(ECar,self).__init__(brand,color,cost_performance)

Python的多重继承虽然增强了扩展性,但如果父类和子类中存在同名方法或变量,则在调用过程中会由于版本、初始化方式、搜索顺序等的不同给程序运行结果带来冲突或不确定性,在实际使用过程中要慎重使用。

可以通过mro()函数看下父类的查找顺序,python3版本采用的是从左到右的顺序查找父类。

print(ECar.mro())
结果如下:
[<class '__main__.ECar'>, <class '__main__.BusCar'>, <class '__main__.SuvCar'>, <class '__main__.Car'>, <class 'object'>]
实例化后,调用print_property方法
ecar = ECar('宋','black','high')
ecar.print_property()

结果如下:依次调用了父类的初始化方法,print_property方法调用的是SuvCar类的方法。

我是ECar类
我是BusCar类
我是SuvCar类
我是父类
宋汽车是SUV

总结

以上介绍的类的三个重要特性封装/多态/继承,相信只是其中一部分知识点,希望达到抛砖引玉的效果,我们大家继续更深层次的学习和探索。

共勉: 东汉·班固《汉书·枚乘传》:“泰山之管穿石,单极之绠断干。水非石之钻,索非木之锯,渐靡使之然也。”

-----指水滴不断地滴,可以滴穿石头;

-----比喻坚持不懈,集细微的力量也能成就难能的功劳。

----感谢读者的阅读和学习,谢谢大家。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

科雷learning

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值