class1 的内容
class class1(object):
"""description of class"""
def __init__(self, restaurant_name):
self.__restaurant_name = restaurant_name
self.__cuisine_type = ["西红柿鸡蛋汤","紫菜蛋花汤","青椒肉丝炒饭"]
self.__number_serevd = 0
def open_restaurant(self):
print(self.__restaurant_name + " is opening now!")
def describe_restaurant(self):
print('Restaurant name is ' + self.__restaurant_name + '.')
print("Menu:")
for i in range(0,len(self.__cuisine_type)):
print(self.__cuisine_type[i])
def set_number_served():
number = input("输入就餐的人数:")
return number
def return_number_severed(self):
return int(self.__number_serevd)
def increment_number_served(self):
__x = set_number_served()
__x = int(__x)
self.__number_serevd = self.__number_serevd + __x
print("当前餐馆服务了的人数为:" + self.__number_serevd)
主程序
from class1 import class1
First=class1('1')
First.describe_restaurant()
First.open_restaurant()
First.increment_number_served()
print(First.return_number_severed())
报错:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File ".\class1.py", line 25, in increment_number_served
__x = set_number_served()
NameError: name 'set_number_served' is not defined
求助!这个为啥错了?
已解决:
原因:
在类内部调用的话,需要加上self. ,否则会被当成外部函数,所以提示未定义