day18 单/多继承的实现

2019-01-16  本文已影响0人  y_j杨健

定义人类

class Person(object):
    def __init__(self,name,age,money):
          self.nama = name
          self.age = age
          self.money = money
    def run(self):
        print('run')

定义学生类:

from person import  Person
class Student(Person):
      def __init__(self,name,age,money)
         #  调用父类中的__init__   super(Student,self).__init__(name,age,money)

定义工人类

from person import Person
class Worker(Person):
      def __init__(self,name,age,money):
                super(Worker,self).__init__(name,age,money)

单继承的实现

from stuendt import Student
from worker import Worker
stu = Student('yangjian',18,100)
print(stu.name,stu.age,stu.money)
stu.run()
stu.eat('alpper')

多继承的实现

定义父亲类

class Father(object):
      def __init__(self,name):
            self.name = name
      def run(self):
            print('run')
      def func(self):
            print('func1')

定义母亲类

class Mother(object):
      def __init__(self,faceValue):
                self.faceValue = faceValue      def  eat(self,food):
              print('eat=' + food)
      def func(self):  
              print('func2')

定义孩子类

from father import Father
from mother import  Mother
class Child(Father,Mother):
        def __init__(self,name,faceValue):
                Father.__init__(self,name)
                Mother.__init__(self,feceValue)      

多继承的实现

from child import Child
c = Child('yangjian',200)
priny(c.name,c.faceValue)
c.run()
c.eat()
c.func()  
父类中方法名相同,默认调用的是在括号排前面的父类中的方法
上一篇下一篇

猜你喜欢

热点阅读