Bootstrap

Python学习笔记之继承

继承

代码的重用

1.单继承
–单继承: 父类放在子类的括号中
–初始化父类:
(1)super().init() 注意括号中没有self,super()是个对象
(2)father.init() 注意这里father是类型

class father:
    def __init__(self):
        self.money=2000e8
    def buy(self):
        print("有钱花")
class son(father):
    def __init__(self):
        #初始化父类
        super().__init__() 
        self.mystr="我不差钱"
    def doing(self):
        print("我是老公")

2.多继承
–多继承 :在子类的括号中放入多个父类,逗号分隔
–属性上是fa2覆盖了fa1 属性的顺序和初始化有关系
fa2.init(self)
fa1.init(self)

fa1.init(self)
fa2.init(self)

得到的属性是不一样的,后者覆盖前者
–#方法上是fa1覆盖了fa2 方法与继承顺序有关 如果是fa2在前就显示fa2的do方法了 前者覆盖后者

class fa1:
    def __init__(self):
        self.money=10000
    def buy(self):
        print("买不起任何东西")
    def do(self):
        print("农民")
class fa2:
    def __init__(self):
        self.money=20000000
    def buy(self):
        print("能买的起任何东西")
    def do(self):
        print("土豪")

class son(fa1,fa2):
    def __init__(self):

        fa2.__init__(self)
        fa1.__init__(self)
    def wanto(self):
        print("一定有钱花")
jt = son()
print(jt.money)

3.继承的局限

下面的无法引用__wife

class fa:
    def __init__(self):
        self.__wwife="娘"#私有变量不可被继承
        self.wife="娘1"
class sons(fa):
    def __init__(self):
        fa.__init__(self)
dawang = fa()
print(dawang.__wwife)# AttributeError: 'fa' object has no attribute '__wwife'
print(dawang.wife)#这个就正常运行

4.Object类
所有类都继承object
5.类的属性

class father1:
    """
我是中国第12345位首富
    """
    def __init__(self):
        self.money=2000e8
        self.mystr='定个小目标'

    def doing(self):
        print("疯狂工作")
    def buy(self):
        print("有钱花")

print(father1.__doc__)#类的说明
print(father1.__name__)#类的名称
print(father1.__module__)#从哪开始执行  __main__
print(father1.__bases__)#类的父类   (<class 'object'>,)
print(father1.__dict__)#类的所有属性放在字典里面

6 .super
–解决多次初始化的问题 节约内存

class ye:
    def __init__(self):
        print("爷构造了一次")
class daerzi(ye):
    def __init__(self):
#         ye.__init__(self)如果是这种的话那就会构造多次
        super().__init__()
        print("大儿子构造了一次")
class ererzi(ye):
    def __init__(self):
        super().__init__()
        print("二儿子构造了一次")
class xiaoerzi(ye):
    def __init__(self):
        super().__init__()
        print("小儿子构造了一次")
class zajiao(daerzi,ererzi,xiaoerzi):
    def __init__(self):
        super().__init__()

xf = zajiao()

7.isinstance与type
判断类型
type 是严格判断

a = 10
print(isinstance(a,int))#返回的是True
print(isinstance(a,str))#False
#如果是子类,类的类型是父类返回的也是true
class A():
    pass
class B(A):
    pass
b = B()
print(isinstance(b,A))#返回的True
print(type(b)==A)#返回的False

8.静态方法与类方法
静态方法用@staticmethod标注 ,无论有没有对象多可以调用 #通用的东西 实不实例化都可以调用
类方法 把类当做参数

def gettall(people):#把类当做参数  类的外部方法
    return people.tall
print(gettall(People(100)))

总结

1.单继承结构: class son(father):
2.多继承结构:class son(father1,father2):
3.多个父类有相同的属性和方法情况下,子类的属性的值取决于初始化的顺序 (父类的init(self)方法的顺序),后者覆盖前者
4.多个父类有相同的属性和方法情况下,子类的方法的结果取决于继承的顺序(class son(fa1,fa2)和 class son(fa2,fa1)),前者覆盖后者
5.继承的局限性 私有变量子类无法使用 father.__wwife 子类无法使用
6.类的属性 doc name module dict bases
7.super 节约资源 如果多个继承 son的每个父类都继承ye 如果在son的父类中初始化ye不用super()就会出现每次都调用一次ye的初始化
8.isinstance与type判断类型,type严格判断
9.静态方法不常用,类方法类当做方法的参数

;