簡體   English   中英

如何從課堂講師訪問課堂學生的變量?

[英]How to access variables of class student from class lector?

我希望st()返回studentname

但是,為什么我得到了空list

如何在lecturer班上獲得student班的名稱?

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']
[]

1)。 lector內部沒有call()函數。 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())

希望對您有幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM