[英]Python, A class within a class, how to access variables from the upper class
[英]How to access variables of class student from class lector?
我希望st()
返回student
的name
。
但是,為什么我得到了空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.