Python--23 类和对象:继承

继承 被继承的类称为基类/父类或超类   继承者称为子类

  class DerivedClassName(BaseClassName):

    ......

>>> class Parent:
...     def hello(self):
...             print('正在调用父类方法...')
... 
>>> class Child(Parent):
...     pass
... 
>>> p = Parent()
>>> p.hello()
正在调用父类方法...
>>> c = Child()
>>> c.hello()
正在调用父类方法...

  如果子类定义与父类同名的方法或属性,则会自动覆盖对应的方法或属性

>>> class Child(Parent):
...     def hello(self):
...             print('正在调用子类的方法')
... 
>>> c = Child()
>>> c.hello()
正在调用子类的方法
>>> p.hello()
正在调用父类方法...

 

import random as r

class Fish:
    def __init__(self):
        self.x = r.randint(0,10)
        self.y = r.randint(0,10)

    def move(self):
        self.x -= 1
        print('我的位置是:',self.x, self.y)

class GoldFish(Fish):
    pass

class Carp(Fish):
    pass

class Salmon(Fish):
    pass

class Shark(Fish):
    def __init__(self):
        self.hungry = True

    def eat(self):
        if self.hungry:
            print('吃货的梦想就是天天有得吃')
            self.hungry = False
        else:
            print('太撑了,吃不下了!')

f = Fish()
f.move()
g = GoldFish()
g.move()
c = Carp()
c.move()
s = Salmon()
s.move()
#sh =Shark()
#sh.move()
#sh.eat()

运行结果

[fengjunjie@localhost ~]$ python3 test.py
我的位置是: 1 4
我的位置是: 0 6
我的位置是: 2 2
我的位置是: 9 6

鲨鱼会调用move会报错,初始化后没有x和y属性

  解决办法

    1 调用未绑定的父类方法

    2 使用supper函数

方式一

import random as r

class Fish:
    def __init__(self):
        self.x = r.randint(0,10)
        self.y = r.randint(0,10)

    def move(self):
        self.x -= 1
        print('我的位置是:',self.x, self.y)

class GoldFish(Fish):
    pass

class Carp(Fish):
    pass

class Salmon(Fish):
    pass

class Shark(Fish):
    def __init__(self):
        Fish.__init__(self)
        self.hungry = True

    def eat(self):
        if self.hungry:
            print('吃货的梦想就是天天有得吃')
            self.hungry = False
        else:
            print('太撑了,吃不下了!')

f = Fish()
f.move()
g = GoldFish()
g.move()
c = Carp()
c.move()
s = Salmon()
s.move()
sh =Shark()
sh.move()
sh.eat()

 

[fengjunjie@localhost ~]$ python3 test.py
我的位置是: 0 6
我的位置是: 0 5
我的位置是: 8 6
我的位置是: 8 0
我的位置是: 9 10
吃货的梦想就是天天有得吃

相当于

  shark = Shark()

  Fish.__init__(shark)

  shark.move()

方式二

import random as r

class Fish:
    def __init__(self):
        self.x = r.randint(0,10)
        self.y = r.randint(0,10)

    def move(self):
        self.x -= 1
        print('我的位置是:',self.x, self.y)

class GoldFish(Fish):
    pass

class Carp(Fish):
    pass

class Salmon(Fish):
    pass

class Shark(Fish):
    def __init__(self):
        super().__init__()
        self.hungry = True

    def eat(self):
        if self.hungry:
            print('吃货的梦想就是天天有得吃')
            self.hungry = False
        else:
            print('太撑了,吃不下了!')

f = Fish()
f.move()
g = GoldFish()
g.move()
c = Carp()
c.move()
s = Salmon()
s.move()
sh = Shark()
sh.move()
sh.eat()

运行结果:

[fengjunjie@localhost ~]$ python3 test.py 

我的位置是: 3 1
我的位置是: 8 1
我的位置是: 8 7
我的位置是: 7 3
我的位置是: 8 7
吃货的梦想就是天天有得吃

多重继承

  class DerivedClassName(Base1, Base2, Base3):

      ......

 

>>> class Base1:
...     def fool1(self):
...             print('我是fool1,我是Base1代言')
... 
>>> class Base2:
...     def fool2(self):
...             print('我是fool2,我为Base2代言...')
... >>> class C(Base1,Base2):
...     pass
... 
>>> c = C()
>>> c.fool1()
我是fool1,我是Base1代言
>>> c.fool2()
我是fool2,我为Base2代言...

 

posted @ 2017-09-11 20:26  110528844  阅读(171)  评论(0编辑  收藏  举报