程序员python 高级码农成才之路

* 禁白嫖 * python ----- 类的继承

2021-02-02  本文已影响0人  幼姿沫

禁白嫖的含义就是

 尊重我的劳动成果  点赞 打赏 转发 谢谢您各位捧场

 #继承 子类继承父类 波斯猫 / 巴厘猫 继承自猫类

class Cat:

    def __init__(self,name,color='white'):

        self.name=name

        self.color=color

    def run(self):

        print('***** %s  is running *****' %self.name)

class BoSi(Cat):

    def setName(self,newName):

       self.name=newName

        return self.name

    def eat(self):

        print('***** %s is eating foods *****' %self.name)

cat=Cat('diandian')

print('初始父类猫的名字为*',cat.name)

print('初始父类猫的颜色为*',cat.color)

cat.run()

print('-----  子类开始继承父类 进行属性和方法的调用 -----')

bosi=BoSi('哈哈')

bosi.setName('湘湘')

print('继承父类波斯猫的名字为*',bosi.name)

print('继承父类波斯猫的颜色为*',bosi.color)

bosi.eat()

bosi.run()

控制台输出成果

初始父类猫的名字为* diandian

初始父类猫的颜色为* white

***** diandian  is running *****

-----  子类开始继承父类 进行属性和方法的调用 -----

继承父类波斯猫的名字为* 湘湘

继承父类波斯猫的颜色为* white

***** 湘湘 is eating foods *****

***** 湘湘  is running *****

'''

私有的属性不可以通过对象直接访问,可以通过方法访问

私有的方法不可以通过对象直接访问

私有的属性和方法 不可以让子类访问继承 是不会对外公布的,往往是用来做内部的事情起到安全的作用

'''

class Animal:

    def __init__(self,name='animal',color='white'):

        self.__name=name

        self.color=color

    def __test(self):

        print('私有的属性 动物名称 *',self.__name)

        print('私有的属性 动物颜色 *',self.color)

    def test(self):

       print('公有的属性 动物名称 *',self.__name)

        print('公有的属性 动物颜色 *',self.color)

class Dog(Animal):

    def dogAppera(self):

        print(self.color)

    def dogAction(self):

        self.test()

animal=Animal()

print(animal.color)

animal.test()

print('*****子类实例化对象继承自父类*****')

dog=Dog(name='ff',color='wonderful')

dog.dogAppera()

dog.dogAction()

控制台输出内容

white

公有的属性 动物名称 * animal

公有的属性 动物颜色 * white

*****子类实例化对象继承自父类*****

wonderful

公有的属性 动物名称 * ff

公有的属性 动物颜色 * wonderful

多继承中父类的公有方法属性 子类都会继承

# 多继承中有一个同名的方法调用的时候会按照逻辑顺序进行调用

class Base:

    def test(self):

        print('father class is Base')

class TestA(Base):

    def test(self):

        print('father class is TestA')

class TestB(Base):

    def test(self):

        print('father class is TestB')

class TestC(TestA,TestB):

    print('TestC already call back the function')

a=TestA()

a.test()

b=TestB()

b.test()

c=TestC()

c.test()

#查看子类的对象搜索方法时的先后顺序

print(TestC.__mro__)

控制台输出结果

TestC already call back the function

father class is TestA

father class is TestB

father class is TestA

(<class '__main__.TestC'>, <class '__main__.TestA'>, <class '__main__.TestB'>, <class '__main__.Base'>, <class 'object'>)

#调用父类方法和属性 也就是继承父类的方法和属性 用super

class Cat:

    def __init__(self,name):

        self.name=name

        self.color='white'

class Bosi(Cat):

    def __init__(self,name):

#调用父类的init方法

        super().__init__(name)

    def getName(self):

        return self.name

    def eat(self):

        return '%s is eating foods' %self.name

bosi=Bosi('扣扣')

print(bosi.color)

print(bosi.getName())

print(bosi.eat())

控制台输出结果

white

扣扣

扣扣 is eating foods

上一篇下一篇

猜你喜欢

热点阅读