面向对象之封装、继承(super)、多态

本文深入探讨了面向对象编程(OOP)的三大核心概念:封装、继承与多态。通过实例详细解析了封装如何保护数据并提供接口,继承如何实现代码复用,以及多态如何提升代码灵活性和可扩展性。

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

一、封装:

一、封装: 属性和方法放到类内部,通过对象访问属性或者方法,隐藏功能的实现过程,不用关心类内部代码的实现的细节。

二、继承的调用

"""
调用父类(超类)方法 
    1.使用父类调用
    2.使用super() 仅适用于新式类  遵循mro顺序
        新式类:在创建的时候继承object (或者是从内置类型,如list,dict等)
        经典类:是直接声明的
        super(A, self).__init__()  A和self可以不写   .__init__()可写参数
        
    3.子类继承父类: 01代码演示
        子类继承父类不做初始化,会自动继承父类属性
        子类继承父类做了初始化,且不调用super初始化父类构造函数,子类不会自动继承父类的属性。
        子类继承父类做了初始化,且调用了super初始化了父类的构造函数,子类也会继承父类的属性
    
    4.子类继承父类: 02代码演示
        初始化并且调用super初始化构造函数,并且重写父类方法
        
    5.super多继承: 03代码演示
        顺序遵循mro
        C只能初始化A 对B没法处理,必须在A中去初始化B
"""

 

二、1代码01

class Person(object):
    def __init__(self,name = "person"):
        self.name = name

class Pupel(Person):  # 不做初始化 会自动继承父类属性 name
    pass

class Puple_init(Person):  # 做初始化  且不调用super初始化父类构造函数 不会继承父类属性 name
    def __init__(self,age):
        self.age = age

class Puple_super(Person):  # 做初始化  也调用super().__init__(name)初始化父类构造函数  会继承父类属性 name
    def __init__(self,name,age):
        self.age = age
        super().__init__(name)

# pp_i = Puple_init(10)
# print(pp_i.age)  # 'Puple_init' object has no attribute 'name'

二、2代码02

def get_logging(word):
    return "notice"

class DWHTaskBase(object):
    def __init__(self, dwf_config):
        self.dwh_config = dwf_config
        
    def notice(self):
        self.logger = get_logging("notice")
        print(self.logger)

class FragmentCreator(DWHTaskBase):
    """
    文章分段任务
    """

    def __init__(self, dwf_config):
        super().__init__(dwf_config)
        self.filter_condition = {
            "fragment": {'$exists': 0},
            'status': 1
        }

        self.update_status = {"fragment": "1"}

    def notice(self):
        # super(FragmentCreator, self).notice()
        results = self.dwh_config.mysql.get_each_data()
        print(results)

# para_creater = FragmentCreator(dwf_config=dwh_config)
# para_creater.notice()

一、3代码03

class A(object):
    def __init__(self):
        print("sds")
        super(A, self).__init__()
        print('>> A')

class B(object):
    def __init__(self):
        # super(B, self).__init__()
        print('>> B')

class C(A, B):
    def __init__(self):
        super(C, self).__init__()
        print('>> C')
# C()
# print(C.mro())

 三、多态


"""
一、
多态概念:
    多态概念:多态是指一类事物有多种形态:(一个抽象类拥有多个子类,故多态依赖继承) 例如动物类包含 猫类
    多态性概念:多态性是指可以通过调用相同的函数名 执行具有不同的函数(实现不同的功能)
    面向对象态性表述多:向不同的对象发送同一条消息,不同的对象在接收时会产生不同的行为(即方法)。也就是说,每个对象可以用自己的方式去响应共同的消息。所谓消息,就是调用函数,不同的行为就是指不同的实现,即执行不同的函数。
多态条件:2个
    1、继承:多态一定是发生在子类和父类之间;   2、重写:子类重写了父类的方法。
"""

class Animal(object):
    def run(self):
        print('Animal is running...')

class Dog(Animal):

    def run(self):
        print('Dog is running...')

class Cat(Animal):

    def run(self):
        print('Cat is running...')


def run_twice(animal):
    animal.run()

run_twice(Dog())

如果觉得不错可以添加关注公众号,不定期分享有趣内容!

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值