人若有情死得早 发表于 2017-7-14 21:39:19

038类和对象:继承

1.继承:一个类使用前面一个类所有的方法和属性
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("太撑了,吃不下了!")

>>> fish = Fish()
>>> fish.move()
我的位置是: 7 6
>>> fish.move()
我的位置是: 6 6
>>> goldfish = Goldfish()
>>> goldfish.move()
我的位置是: 9 7
>>> goldfish.move()
我的位置是: 8 7
>>> goldfish.move()
我的位置是: 7 7
>>> shark = Shark()
>>> shark.eat()
吃货的梦想就是天天有的吃^_^
>>> shark.eat()
太撑了,吃不下了!
>>> shark.move()
Traceback (most recent call last):
File "<pyshell#11>", line 1, in <module>
    shark.move()
File "F:\BaiduYunDownload\《零基础入门学习Python》\py\038-1.py", line 8, in move
    self.x -= 1
AttributeError: 'Shark' object has no attribute 'x'

1)调用未绑定的父类方法
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("太撑了,吃不下了!")

>>> shark = Shark()
>>> shark.move()
我的位置是: 3 1
>>> shark.move()
我的位置是: 2 1
>>> Fish.__init__()
Traceback (most recent call last):
File "<pyshell#15>", line 1, in <module>
    Fish.__init__()
TypeError: __init__() missing 1 required positional argument: 'self'
>>> Fish.__init__(shark)
>>> shark.move()
我的位置是: 6 4
2)使用super函数
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("太撑了,吃不下了!")
>>> shark = Shark()
>>> shark.move()
我的位置是: 3 3
2.多重继承:可以同时继承多个父类的属性和方法
class DerivedClassName(Base1, Base2, Base3):
                  ......
>>> class Base1:
        def foo1(self):
                print("我是foo1,我为Base1代言...")

               
>>> class Base2:
        def foo2(self):
                print("我是foo2,我为Base2代言...")

               
>>> class C(Base1, Base2):
        pass

>>> c = C()
>>> c.foo1()
我是foo1,我为Base1代言...
>>> c.foo2()
我是foo2,我为Base2代言...
页: [1]
查看完整版本: 038类和对象:继承