美文网首页
day18 单/多继承的实现

day18 单/多继承的实现

作者: y_j杨健 | 来源:发表于2019-01-16 00:40 被阅读0次

    定义人类

    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()  
    父类中方法名相同,默认调用的是在括号排前面的父类中的方法
    

    相关文章

      网友评论

          本文标题:day18 单/多继承的实现

          本文链接:https://www.haomeiwen.com/subject/wpfpdqtx.html