继承,多继承,单继承

继承
子类以及子类实例化的对象可以父类的任何方法或变量
类名可以访问父类所有内容
class Animal:
    def __init__(self,name,sex,age):
        self.name=name
        self.sex=sex
        self.age=age
    def eat(self,a1):
        print('%s吃%s' % (self.name,a1))
    def drink(self):
        print('%s喝东西' %(self.name))
class Cat(Animal):
    def miaow(self):
        print('')
class Brid(Animal):
    def __init__(self,name,sex,age,wing):
        super().__init__(name,sex,age)
        self.wing=wing
    def bark(self):
        print('啾啾')
    def eat(self,b2):
        super().eat(b2)     #super()拿到父类中的这个方法,然后执行父类中的这个方法,同事也执行子类中的这个方法
        print('狗狗在吃饭')
#既执行父类中的这个方法又执行子类中的这个方法,父类和子类中必须都有这个方法
a1=Brid('小鸡','',2,'kan')
a1.eat('虫子')        #这个参数是传给父类里面eat这个方法的

#执行子类中的方法
cat1=Cat('小白','',2)
cat1.miaow()

#只执行父类的方法,子类中不要定义与父类同样的方法
#只执行子类中的方法,父类中不要定义与子类同样的方法
继承:单继承,多继承
类:新式类,经典类
新式类:凡是继承object类的都是新式类
Python3 中都是新式类,因为Python3 中类都默认继承object
经典类:不继承object类的都是经典类
Python2 (既有新式类,又有经典类)所有的类默认都不继承object类,所有的类默认都是经典类,你可以让其继承object.
单继承:新式类,经典类查询顺序一样

class A:
    pass
    def func(self):
        print('in a')
class  B(A):
    pass
    def func(self):
        print('in b')
class C(B):
    pass
    def func(self):
        print('in c')
c1=C()
c1.func()
多继承:
新式类:遵循广度优先
经典类:遵循深度优先


待续.....

猜你喜欢

转载自www.cnblogs.com/hdy19951010/p/9398459.html