Discuss / Python / 请问这个哪里错了??

请问这个哪里错了??

Topic source

class Student(object):

    def __init__(self, name, gender):

        self.name = name

        self.gender = gender

    def print_gender(self):

        print('%s:%s' %(self.__gender,self.__name))

    def get_gender(self):

        return self.__gender

    def set_gender(self):  #参数检查我没写,涉及到字符串的参数检查我不会

        self.__gender=gender

UTOO_NAIVE

#2 Created at ... [Delete] [Delete and Lock User]

有两个问题:

1、初始化私有变量前要加下划线

 def __init__(self, name, gender):
        self.name = name      # 改为 self.__name = name
        self.gender = gender  # 改为 self.__gender = gender

2、set函数参数缺少形参gender

def set_gender(self):  # 改为 def set_gender(self, gender): 形参缺少函数内部上哪找变量‘gender’?
        self.__gender=gender

  • 1

Reply