创建一个名为User的类,其中包含属性first_name和last_name,还有用户简介通常会存储的其他几个属性。在类User中定义一个名为describe_user()的方法,它打印用户信息摘要;再定义一个名为greet_user()的方法,它向用户发出个性化的问候。
# -*- conding:utf-8 -*-
class User:
def __init__(self,first_name,last_name,age,sex,phone,login_attempts = 0):
self.first_name = first_name
self.last_name = last_name
self.age = age
self.sex = sex
self.phone = phone
self.login_attempts=login_attempts
def describe_user(self):
print('我叫 %s %s,今年%d岁,我的电话是%s'%(self.first_name,self.last_name,self.age,self.phone))
def greet_user(self):
print('尊敬的%s,您好!'%self.first_name)
#递增登录次数
def increment_login_attempts(self):
self.login_attempts += 1
print('%s的登录次数为:%d'%(self.first_name,self.login_attempts))
#重置登录次数
def reset_login_attempts(self):
self.login_attempts = 0
print('%s的登录次数为:%d' % (self.first_name, self.login_attempts))
if __name__ == '__main__':
joe = User('Joe','Black',20,'男','18011123333')
#joe.describe_user()
# joe.greet_user()
joe.increment_login_attempts()
joe.increment_login_attempts()
joe.increment_login_attempts()
joe.increment_login_attempts()
joe.reset_login_attempts()