python_oop 封装-继承-多态

复制代码
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:woshinidaye
#OOP:object-oriented programming

'''
class People(object):           #新式写法
#class People:                  #经典写法
    n = 123                 #类变量
    def __init__(self,name,age):        #传参
        self.name = name
        self.age = age
    def eating(self):
        print(f'{self.name} is eating....')

one_person = People('woshinidaye',18)               #实例化
one_person.eating()

#查
print(one_person.name,one_person.age)   #查看实例化中的参数
print(one_person.n)                 #没有在实例中增加n,就查询类中的变量

#增
one_person.male = 'man'
print(one_person.male)
one_person.n = 'ABC'                    #在实例中新增一个n,之后的增删改查均是对实例的这个n进行修改
print(one_person.n)

#删
del one_person.male
# print(one_person.male)          #已经被删除,会报错

#改
one_person.age = 123456789
print(one_person.name,one_person.age)

class man(People):         #继承
    def test(self):
        print(f'{self.name}喜欢拉屎~~~')

one_man = man('woshinanren',16)
one_man.eating()                        #子类继承父类的eat特性,
one_man.test()                  #正常调用子类的特性

class woman(People):
    def __init__(self,name,age,shopping_place,shopping_class):   #子类中重新传入参数,如果要继承的话,需要把父类中的参数都一并传入
        self.name = name
        self.age =  age
        # People.__init__(self,name,age)         #经典写法
        #super().__init__(name,age)      #新式写法,直接继承父类的参数
        self.shopping_place = shopping_place
        self.shopping_class = shopping_class
    def shopping(self):
        print(f'{self.name} is loving shopping.....in '
              f'{self.shopping_place}....buying {self.shopping_class}')

one_woman = woman('nidenvren',123,'taiguli','化妆品')
one_woman.shopping()
#多继承
class worker(object):           #新建一个父类
    # def __init__(self,one,two):       #为了区别多继承时的传参顺序差异
    #     self.one = one
    #     self.two = two
    def get_work(self,obj):
        print(f'{self.name} is working.... {obj.name}....')

class new_man(People,worker):       #多继承,new_man这个子类,同时继承People和worker两个父类的属性,
    # class new_man(worker,People):  #多继承是有顺序区别的,如果worker和people的传参不一样的话;
    #为了避免多继承时,传的参数有差别,可以在多继承时,再次传入参数,这样也避免了父类的顺序差异问题;
    def __init__(self,name,age):
        People.__init__(self,name,age)
    def new_test(self):
        print('test')

new_man_test = new_man('adfadf',576890)  #实例化,如果在子类中没有传参,传的参数会被传到People中的__init__,
# 如果子类中有传参数,就会被传到子类中
new_man_test.new_test()
new_man_test.get_work(one_woman)
'''



#多继承的顺序问题
class func_A(object):       #A是老父亲
    def __init__(self):
        print('in the A')
class func_B(func_A):       #B继承A
    pass
    # def __init__(self):
    #     print('in the B')
class func_C(func_A):       #C继承A
    pass
    # def __init__(self):
    #     print('in the C')
class func_D(func_B,func_C):
    pass

test = func_D()
# python3中经典类和新式类都是按照广度优先来继承:
结论是   先继承B--->继承C--->继承A    python3中为广度优先

# python2中经典类是按照深度优先来继承,新式类是按照广度优先来继承
结论是   先继承B--->继承A--->继承C    python2中为深度优先




class schoolmember(object):
    def __init__(self,name,age):
        self.name = name
        self.age = age
    def test(self):
        print(f'{self.name} 已经 {self.age} 了')
class student(schoolmember):
    def __init__(self,name,age,course):
        schoolmember.__init__(self,name,age)    #经典写法
        # super().__init__(name,age)            #新式写法
        self.course = course
    def stu_course(self,lonth):
        print(f'我的课程是:{self.course},课时长度为{lonth}分钟')

stu_test = student('woshinidaye',23,'english')
print(stu_test.course)
stu_test.stu_course(50)

#多态,一个接口 多个实现      允许将子类类型的指针赋值给父类类型的指针

class Animal(object):
    def __init__(self,animal_class):
        self.animal_class =animal_class

    @staticmethod
    def animal_talk(obj):
        obj.talk()

class cat(Animal):
    def talk(self):
        print(f'{self.animal_class} 正在大叫!!!  meow~~  meow~~~')

class dog(Animal):
    def talk(self):
        print(f'{self.animal_class} 正在大叫!!!  wang~~~ wang~~~')

#正常流程,先对子类实例化
'''
one_cat = cat('my_cat')
one_cat.talk()
one_dog = dog('my_dog')
one_dog.talk()
'''
#为了实现多态
#先定义一个叫的函数,并实例化对象
'''
def animal_talk(obj):
    obj.talk()
one_cat = cat('my_cat')
one_dog = dog('my_dog')
animal_talk(one_dog)
animal_talk(one_cat)
'''
#能不能把上述的函数合到父类中,从父类实现调用。
one_cat = cat('my_cat')
one_dog = dog('my_dog')
Animal.animal_talk(one_cat)
Animal.animal_talk(one_dog)
复制代码

 

posted @   woshinidaye  阅读(41)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· Qt个人项目总结 —— MySQL数据库查询与断言
点击右上角即可分享
微信分享提示