python之面向对象

目录

self指的是调⽤该函数的对象

魔法方法

继承

super()调⽤⽗类⽅法

私有属性和方法


self指的是调⽤该函数的对象

class Washer():
    def wash(self):
        print(self)
        print('洗衣服')
wash=Washer()
print(wash)
wash.wash()
'''
<__main__.Washer object at 0x0000018876BD7B80>
<__main__.Washer object at 0x0000018876BD7B80>
洗衣服
'''

对象属性

类外⾯添加对象属性: 外面实例引用直接.xx赋值

haier1.width = 500

haier1.height = 800

类外⾯获取对象属性

print(f'haier1洗⾐机的宽度是{haier1.width}')

类⾥⾯获取对象属性 使用self.xx进行获取或者赋值

self.属性名

魔法方法

__init__()

class Washer():
    def __init__(self,width,height):
        self.width=width
        self.height=height

    def print_info(self):
        print(f'h宽度{self.width},h高度{self.height}')
h=Washer(100,200)
h.print_info()

__str__()

当使⽤print输出对象的时候,默认打印对象的内存地址。如果类定义了__str__⽅法,那么就会打印从在这个⽅法中 return 的数据。

说白了 就是给self 和实例对象重命名 可以用来 把输出的数据直接定义再str中方便调用输出

class Washer():
    def __init__(self,width,height):
        self.width=width
        self.height=height
    def __str__(self):
         return '这是此对象的说明'
    def print_info(self):
        print(f'h宽度{self.width},h高度{self.height}')
h=Washer(100,200)
h.print_info()
print(h) #默认 <__main__.Washer object at 0x00000246ECC978B0>
#使用了 str魔法后 打印return的数据

__del__():

当删除对象时,python解释器也会默认调⽤__del__()⽅法。

class Washer():
    def __init__(self,width,height):
        self.width=width
        self.height=height
    def __str__(self):
         return 'washer'
    def __del__(self):
        print(f'{self}对象已经被删除')
    def print_info(self):
        print(f'h宽度{self.width},h高度{self.height}')
h=Washer(100,200)
h.print_info()
print(h)
'''
h宽度100,h高度200
washer
washer对象已经被删除
'''

烤地瓜例子

class SweetPotato:
    def __init__(self):
        self.cook_time = 0
        self.cook_static = '生的'
        # 调料
        self.cook_condiments = []

    def cook(self, time):
        self.cook_time += time
        if 0 <= self.cook_time <= 3:
            self.cook_static = '生的'
        elif 3 < self.cook_time <= 8:
            self.cook_static = '半生不熟'
        elif 8 < self.cook_time <= 10:
            self.cook_static = '熟了'
        else:
            self.cook_static = '糊了'

    def add_condiments(self, condiment):
        self.cook_condiments.append(condiment)

    def __str__(self):
        return f'这个地瓜烤了{self.cook_time}分钟,{self.cook_static},添加的调料{self.cook_condiments if len(self.cook_condiments) > 0 else 0}'


s = SweetPotato()
print(s)
s.cook(2)
s.add_condiments('酱油')
s.cook(10)
s.add_condiments('辣椒')
print(s)
'''
这个地瓜烤了0分钟,生的,添加的调料0
这个地瓜烤了12分钟,糊了,添加的调料['酱油', '辣椒']
'''

继承

⼦类默认继承⽗类的所有属性和⽅法

简单继承

class A:
    def __init__(self):
        self.num=1
    def info_print(self):
        print(self.num)
class B(A):
    pass
B().info_print() #1
print(B().num)#1

多继承

注意 当⼀个类有多个⽗类的时候,默认使⽤第⼀个⽗类的同名属性和⽅法。

class A:
    def __init__(self):
        self.name='zndroid'
    def info_print(self):
        print(f'名字是{self.name}')
    def info_p(self):
        print(f'A类info_p打印的名字是{self.name}')
class B:
    def __init__(self):
        self.name='haha'
    def info_print(self):
        print(f'名字是{self.name}')
class C(A,B):
    pass
C().info_print()
C().info_p()
'''
名字是zndroid
A类info_p打印的名字是zndroid
'''

重写⽗类同名⽅法和属性  默认使⽤⼦类的同名属性和⽅法

说白了 爹有的儿子也有的  儿子用自己的 这样才能进化

调用父类同名属性或者方法

父类名称.xxx

为保证调⽤到的也是⽗类的属性,必须在调⽤⽅法前调⽤⽗类的初始化

另外自身的其他方法若调用到自身与父类同名属性的话 也要调用自身的__init()

因为python中一旦调用过父类同名属性和方法 此时再调用自身的同名属性和方法的话,

若不去自身初始化init 会导致调用的结果为上次调用父类同名属性和方法的结果

class Master(object):
    def __init__(self):
        self.kongfu = '[古法煎饼果⼦配⽅]'

    def make_cake(self):
        print(f'运⽤{self.kongfu}制作煎饼果⼦')


class School(object):
    def __init__(self):
        self.kongfu = '[⿊⻢煎饼果⼦配⽅]'

    def make_cake(self):
        print(f'运⽤{self.kongfu}制作煎饼果⼦')


class Person(Master, School):
    def __init__(self):
        self.kongfu = '[独创煎饼果⼦配⽅]'

    def make_cake(self):
        #若这里不初始化 最后一次再调用此方法的时候 结果会是
        #运⽤[⿊⻢煎饼果⼦配⽅]制作煎饼果⼦
        self.__init__()
        print(f'运⽤{self.kongfu}制作煎饼果⼦')

    def make_master_cake(self):
        Master.__init__(self)
        Master.make_cake(self)
    def make_school_cake(self):
        School.__init__(self)
        School.make_cake(self)


p = Person()
p.make_cake()
p.make_master_cake()
p.make_school_cake()
p.make_cake()
'''
运⽤[独创煎饼果⼦配⽅]制作煎饼果⼦
运⽤[古法煎饼果⼦配⽅]制作煎饼果⼦
运⽤[⿊⻢煎饼果⼦配⽅]制作煎饼果⼦
运⽤[独创煎饼果⼦配⽅]制作煎饼果⼦
'''

多层继承

class Master(object):
    def __init__(self):
        self.kongfu = '[古法煎饼果⼦配⽅]'

    def make_cake(self):
        print(f'运⽤{self.kongfu}制作煎饼果⼦')


class School(object):
    def __init__(self):
        self.kongfu = '[⿊⻢煎饼果⼦配⽅]'

    def make_cake(self):
        print(f'运⽤{self.kongfu}制作煎饼果⼦')


class Person(Master, School):
    def __init__(self):
        self.kongfu = '[独创煎饼果⼦配⽅]'

    def make_cake(self):
        # self.__init__()
        print(f'运⽤{self.kongfu}制作煎饼果⼦')

    def make_master_cake(self):
        Master.__init__(self)
        Master.make_cake(self)
    def make_school_cake(self):
        School.__init__(self)
        School.make_cake(self)

class Tusun(Person):
    pass
p = Person()
p.make_cake()
p.make_master_cake()
p.make_school_cake()

t=Tusun()
t.make_cake()
t.make_master_cake()
t.make_school_cake()
'''
运⽤[独创煎饼果⼦配⽅]制作煎饼果⼦
运⽤[古法煎饼果⼦配⽅]制作煎饼果⼦
运⽤[⿊⻢煎饼果⼦配⽅]制作煎饼果⼦

运⽤[独创煎饼果⼦配⽅]制作煎饼果⼦
运⽤[古法煎饼果⼦配⽅]制作煎饼果⼦
运⽤[⿊⻢煎饼果⼦配⽅]制作煎饼果⼦
'''

还有一种多层继承方式  >继承  a>b  a>c b,c>d

class A:
    def __init__(self):
        print('a')
class B(A):
    def __init__(self):
        print('b')
class C(A):
    def __init__(self):
        print('c')
class D(B,C):
    pass
D() #b
class A:
    def __init__(self):
        print('a')
class B(A):
    def __init__(self):
        print('b')
class C(A):
    def __init__(self):
        print('c')
class D(C,B):
    pass
D() #c
print(D.__mro__) #查看继承关系
#(<class '__main__.D'>, <class '__main__.C'>, 
# <class '__main__.B'>, <class '__main__.A'>, <class 'object'>)

说明一点   若出现多个继承且调用方法也是一致的情况下,此时优先调用继承顺序先后

类名.__mro__ 查看继承关系

super()调⽤⽗类⽅法

调用父类方法 有三种方式

第一种:父类名.xx(),若该方法涉及到定义的属性或者方法定义在另外的父类方法中,此方法也要调用

第二种super(当前类名,self).函数() 此时只会延伸到该类的上一级父类 若还想连续调用上上级,则还需再该类上级的方法中添加super(当前类名,self).函数()

第三种  super().函数()

class Master(object):
    def __init__(self):
        self.kongfu = '[古法煎饼果子配方]'

    def make_cake(self):
        print(f'运用{self.kongfu}制作煎饼果子')


class School(Master):
    def __init__(self):
        self.kongfu = '[黑马煎饼果子配方]'

    def make_cake(self):
        print(f'运用{self.kongfu}制作煎饼果子')

        # 2.1 super()带参数写法
        # super(School, self).__init__()
        # super(School, self).make_cake()

        # 2.2 无参数的super
        super().__init__()
        super().make_cake()


class Prentice(School):
    def __init__(self):
        self.kongfu = '[独创煎饼果子技术]'

    def make_cake(self):
        self.__init__()
        print(f'运用{self.kongfu}制作煎饼果子')

    # 子类调用父类的同名方法和属性:把父类的同名属性和方法再次封装
    def make_master_cake(self):
        Master.__init__(self)
        Master.make_cake(self)

    def make_school_cake(self):
        School.__init__(self)
        School.make_cake(self)

    # 需求:一次性调用父类School Master的方法
    def make_old_cake(self):
        # 方法一:如果定义的类名修改,这里也要修改,麻烦; 代码量庞大,冗余
        # School.__init__(self)
        # School.make_cake(self)
        # Master.__init__(self)
        # Master.make_cake(self)

        # 方法二:super()
        # 2.1 super(当前类名, self).函数()
        # super(Prentice, self).__init__()
        # super(Prentice, self).make_cake()

        # 2.2 无参数super
        super().__init__()
        super().make_cake()


daqiu = Prentice()

daqiu.make_old_cake()
'''
运用[黑马煎饼果子配方]制作煎饼果子
运用[古法煎饼果子配方]制作煎饼果子
'''

切记  再同名属性和方法中 一定要看清调用的方法中是否涉及到同名属性,初始化或定义该属性的方法也要一起去调用 否则就会出现上次调用的结果

错误的例子:

class Master(object):
    def __init__(self):
        self.kongfu = '[古法煎饼果子配方]'

    def make_cake(self):
        print(f'运用{self.kongfu}制作煎饼果子')


class School(Master):
    def __init__(self):
        self.kongfu = '[黑马煎饼果子配方]'

    def make_cake(self):
        print(f'运用{self.kongfu}制作煎饼果子')

        # 2.1 super()带参数写法
        # super(School, self).__init__()
        # super(School, self).make_cake()

        # 2.2 无参数的super
        super().__init__()
        super().make_cake()


class Prentice(School):
    def __init__(self):
        self.kongfu = '[独创煎饼果子技术]'

    def make_cake(self):
        self.__init__()
        print(f'运用{self.kongfu}制作煎饼果子')

    # 子类调用父类的同名方法和属性:把父类的同名属性和方法再次封装
    def make_master_cake(self):
        Master.__init__(self)
        Master.make_cake(self)

    def make_school_cake(self):
        # 这里不init
        super().make_cake()

    # 需求:一次性调用父类School Master的方法
    def make_old_cake(self):
        # 方法一:如果定义的类名修改,这里也要修改,麻烦; 代码量庞大,冗余
        # School.__init__(self)
        # School.make_cake(self)
        # Master.__init__(self)
        # Master.make_cake(self)

        # 方法二:super()
        # 2.1 super(当前类名, self).函数()
        # super(Prentice, self).__init__()
        # super(Prentice, self).make_cake()

        # 2.2 无参数super
        super().__init__()
        super().make_cake()


daqiu = Prentice()

daqiu.make_old_cake()
# daqiu.make_cake()
daqiu.make_school_cake()
'''
运用[黑马煎饼果子配方]制作煎饼果子
运用[古法煎饼果子配方]制作煎饼果子
运用[古法煎饼果子配方]制作煎饼果子
运用[古法煎饼果子配方]制作煎饼果子
'''
class Master(object):
    def __init__(self):
        self.kongfu = '[古法煎饼果子配方]'

    def make_cake(self):
        print(f'运用{self.kongfu}制作煎饼果子')


class School(Master):
    def __init__(self):
        self.kongfu = '[黑马煎饼果子配方]'

    def make_cake(self):
        print(f'运用{self.kongfu}制作煎饼果子')

        # 2.1 super()带参数写法
        # super(School, self).__init__()
        # super(School, self).make_cake()

        # 2.2 无参数的super
        super().__init__()
        super().make_cake()


class Prentice(School):
    def __init__(self):
        self.kongfu = '[独创煎饼果子技术]'

    def make_cake(self):
        self.__init__()
        print(f'运用{self.kongfu}制作煎饼果子')

    # 子类调用父类的同名方法和属性:把父类的同名属性和方法再次封装
    def make_master_cake(self):
        Master.__init__(self)
        Master.make_cake(self)

    def make_school_cake(self):
        # 这里不init
        super().make_cake()

    # 需求:一次性调用父类School Master的方法
    def make_old_cake(self):
        # 方法一:如果定义的类名修改,这里也要修改,麻烦; 代码量庞大,冗余
        # School.__init__(self)
        # School.make_cake(self)
        # Master.__init__(self)
        # Master.make_cake(self)

        # 方法二:super()
        # 2.1 super(当前类名, self).函数()
        # super(Prentice, self).__init__()
        # super(Prentice, self).make_cake()

        # 2.2 无参数super
        super().__init__()
        super().make_cake()


daqiu = Prentice()

daqiu.make_old_cake()
daqiu.make_cake()
daqiu.make_school_cake()
'''
运用[黑马煎饼果子配方]制作煎饼果子
运用[古法煎饼果子配方]制作煎饼果子
运用[独创煎饼果子技术]制作煎饼果子
运用[独创煎饼果子技术]制作煎饼果子
运用[古法煎饼果子配方]制作煎饼果子
'''

私有属性和方法

私有属性和私有⽅法只能在该类⾥⾯访问和修改

不能被继承

若想修改 再该类中定义公共的修改或者获取该私有属性或者方法的方法让其子类去继承使用

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值