首页 > 解决方案 > 如何从班级讲师访问班级学生的变量?

问题描述

我想st()退回student's 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())

结果:

['david', 'nick']
[]

标签: pythonpython-3.xoop

解决方案


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())

我希望它会帮助你。


推荐阅读