如何从课堂讲师访问课堂学生的变量?

时间:2019-06-23 15:10:45

标签: python python-3.x oop

我希望st()返回student的{​​{1}}。

但是为什么我会得到空的name

如何在list类中获得student类的名称?

lecturer

结果:

class student:
    def __init__(self,*name):
        self.name=list(name)

    def stud(self):
        return self.name

stu=student("david","nick")
print(stu.stud())

class lector(student):
    def st(self):
        return self.name

lec=lector()
print(lec.st())

1 个答案:

答案 0 :(得分:0)

1)。类call()中没有lector函数。 2)。我已经修改了您的代码以获得结果:-

class student:

    def __init__(self,*name):
        student.name=list(name)

    def stud(self):
        return student.name
stu=student("david","nick")
print(stu.stud())

class lector(student):
    def __init__(self):  # Now you can access the variable which are inside student classs
        super
    def st(self):       
        return lector.name

lec=lector()
print(lec.st())

希望它能对您有所帮助。