class student:
birth_day = 21
birth_month = 4
birth_year = 1998
def __init__(self,name):
self.naav = name
def SayHi(self):
return print('hello'+''+self.naav)
Topper = student('vikas')
print(Topper.naav)
print(Topper.SayHi())
print(student.birth_day)
print(Topper.birth_day)
#print(student.naav)
这个的输出是
vikas
hellovikas
None
21
21
我对第三输出“无”感到困惑,不知道它是怎么工作的,有人帮我理解吗
这是因为在
print(Topper.SayHi())
上,您正在打印函数SayHi
返回的内容。但是函数不返回任何内容(None)。 这是因为print('hello'+''+self.naav)
不返回值,它做了一些事情,什么也不返回。 您应该做的是只返回字符串(更改我刚才提到的这一行),然后打印函数SayHi的返回(正如您已经在做的那样)。你知道吗如果我不清楚或者你还不知道该怎么办,告诉我。你知道吗
相关问题 更多 >
编程相关推荐