python_day21

# __init__ 魔术方法  构造方法
'''
实例化对象,初始化的时候触发
功能,为对象添加成员
参数:不固定
返回值:无

'''

#1.基本语法
class MyClass():
    def __init__(self):
        print("构造方法被触发")
        self.color = "blue"

#实例化对象
obj = MyClass()
print(obj.__dict__)
print(obj.color)

#带有多个参数的构造方法
class MyClass():
    def __init__(self,color):
        self.color = color

obj = MyClass("blue")
print(obj.color)

#类可以是一个,对象可以是多个,创造的对象是独立的
class Children():
    def __init__(self,name,skin):
        self.name = name
        self.skin = skin
    def cry(self):
        print("cry")
    def laugh(self):
        print("laugh")
    def __eat(self):
        print("eat")
    def info(self):
        print("{},{}".format(self.name,self.skin))

#实例化对象
afanda = Children("阿凡达","bule")
haoke = Children("霍可","yellow")

#继承

'''
一个类除了自身拥有的属性方法外,还获取了另一个类的成员属性的方法
被继承的类叫做弗雷(基类,超类),继承的类叫做子类(衍生类)
在python中的所有类都继承object这个父类
继承  1.单继承  2.多继承

'''
#单继承
class Human(object):
    eye = "black"

    def climb(self):
        print("爬山")
    def run(self):
        print("跑步")
    def __eat(self):
        print("吃饭")

#1.子夫继承之后,子类可以调用父类的公有成员
class Man(Human):
    pass
obj = Man()
obj.climb()
#子类继承之后,子类不能调用父类的私有成员
#子父继承后,子类可以重写父类同名的公有方法
class Children(Human):
    def run(self):
        print("跑的更快")
obj = Children()
obj.run()

#多继承
#基本语法
class Father():
    property = "rich"
    def f_hobby(self):
        print("hobby")


class Mother():
    property = "beautiful"
    def m_hobby(self):
        print("like")

class daughter(Father,Mother):
    pass
obj = daughter()
obj.property     

class Father():
    property = "rich"
    def f_hobby():
        print("hobby")


class Mother():
    property = "beautiful"
    def m_hobby():
        print("like")

'''
super 本身是一个类 super()是一个对象,用于调用父类的绑定方法
super() 只应用在绑定方法中,默认自动传递self对象,前提(super所在的作用域存在self)
super用途  解决复杂的多继承调用顺序
'''        

class Son(Father,Mother):
    property = "game"
    #类调用
    def skill1(self):
        Father.f_hobby()
    #对象成员调用
    '''按照顺序找:对象本身=>类=>父类'''
    def skill2(self):
        print(self.property)
    #用super调用成员 只调用父类的相关成员
    def skill3(self):
        super().property
        super().m_hobby()

obj2 = Son()
obj2.skill1()
print("<========>")
#菱形继承(钻石继承)
class Human():
    def feel(self):
        print("happy")
        print(self)

class Man(Human):
    def feel(self):
        print("sad")
        super().feel()

class Woman(Human):
    def feel(self):
        print("cold")
        super().feel()

class Children(Man,Woman):
    def feel(self):
        print("cool")
        super().feel()
    
obj = Children()
obj.feel()
'''
#mor 列表
m method
r resolution
o order
super 会自动根据mor列表饭返回出的顺序以此进行调用
super 作用解决复杂的多继承调用顺序  依照mor返回的列表顺序,依次调用
super 调用的顺序:会按照c3算法的广度优先选择进行调用
super传参  会默认在调用方法时,传递参数对象
'''
lst = Children.mro()
print(lst)

#issubclass 判断类的子父关系(应用在类与类)

#isinstance 判断对象类型(应用在类与对象)

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值