Problems with python class inheritance
本问题已经有最佳答案,请猛点这里访问。
我在玩弄Python类继承,但似乎无法让子类正常工作。
我得到的错误消息是:
1 | must be a type not classobj |
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | class User(): """Creates a user class and stores info""" def __init__(self, first_name, last_name, age, nickname): self.name = first_name self.surname = last_name self.age = age self.nickname = nickname self.login_attempts = 0 def describe_user(self): print("The users name is" + self.name.title() +"" + self.surname.title() +"!") print("He is" + str(self.age) +" year's old!") print("He uses the name" + self.nickname.title() +"!") def greet_user(self): print("Hello" + self.nickname.title() +"!") def increment_login_attempts(self): self.login_attempts += 1 def reset_login_attempts(self): self.login_attempts = 0 class Admin(User): """This is just a specific user""" def __init__(self, first_name, last_name, age, nickname): """Initialize the atributes of a parent class""" super(Admin, self).__init__(first_name, last_name, age, nickname) jack = Admin('Jack', 'Sparrow', 35, 'captain js') jack.describe_user() |
我用的是python 2.7
如果以后要调用
1 2 3 | class User(object): ... |
python2区分了不从