麻烦大神看一下,为什么代码运行后显示Skill不存在?(已解决)

来源:3-4 Python中的多态

慕妹6092760

2021-08-23 23:27

class Person(object):

    def __init__(self, name, age):

        self.name = name

        self.age = age

class Student(Person):

    def __init__(self, name, age, grade):

        super(Student, self).__init__(name, age)

        self.grade = grade

class Teacher(Person):

    def __init__(self, name, age, subject):

        super(Teacher,self).__init__(name,age)

        self.subject = subject

class Skill(object):

    def __init__(self,skill):

        self.skill = skill

class Basketball(Skill):

    def __init__(self,skill, level):

        super(Basketball, self).__init__(skill)

        self.level = level

class Football(Skill):

    def __init__(self,skill,level):

        super(Football, self).__init__(skill)

        self.level=level

class BK(Student,Basketball):

    def __init__(self,name,age,grade,skill,level,practise_time):

      Student.__init__(self,name, age, grade)

      Basketball.__init__(self, skill, level)

 #  super(BK,self).__init__(name,age,grade,skill,level)(原来是这样写的,外部调用不了第二父类的属性)

        BK.practise_time=practise_time

class FT(Teacher,Football):

    def __init__(self,name,age,subject,skill,level,practise_time):

       Teacher.__init__(self,name,age,subject)

       Football.__init__(self,skill,level)

# super(FT,self).__init__(name,age,subject,skill,level)

        FT.practise_time = practise_time

a = BK('LiMing', 13, 13,'Basketball', 3, 6)

b = FT('LiLei', 40, 'Math', 'Football',6,20)

print(a.age)

print(b.skill)


写回答 关注

2回答

  • hermaniu
    2021-11-23 14:02:25

    外部调用不了第二父类的属性,醍醐灌顶!

  • 慕瓜0436584
    2021-09-01 02:53:21

    代码可以运行,要注意class BK和class FT这两个类里面__init__()方法代码的空格,空格要统一

Python3 进阶教程(新版)

学习函数式、模块和面向对象编程,掌握Python高级程序设计

41910 学习 · 236 问题

查看课程

相似问题