''' class user '''
class User():
''' define User class to store user's info '''
def __init__(self,first_name,last_name,sex,age):
self.first_name = first_name
self.last_name = last_name
self.sex = sex
self.age = age
def describe_user(self):
print(
self.first_name.title() + ' ' +
self.last_name.title() + ' ' +
str(self.age) +' ' +self.sex
)
def greet_user(self):
print('Hello, '+ self.first_name)
# 9-3
from user import User
user0 = User('lily','Green','female',20)
user1 = User('mark','kim','male',19)
user2 = User('joyce','potter','female',10)
users = [user0,user1,user2]
for user in users:
user.describe_user()
user.greet_user()
输出:
# 9-5
class User():
''' define User class to store user's info '''
def __init__(self,first_name,last_name,sex,age):
self.first_name = first_name
self.last_name = last_name
self.sex = sex
self.age = age
self.login_attempts = 0
def describe_user(self):
print(
self.first_name.title() + ' ' +
self.last_name.title() + ' ' +
str(self.age) +' ' +self.sex
)
def greet_user(self):
print('Hello, '+ self.first_name)
def increment_login_attempts(self):
self.login_attempts += 1
def reset_login_attempts(self):
self.login_attempts = 0
user0 = User('lily','green','female',20)
for i in range(0,10):
user0.increment_login_attempts()
print(user0.login_attempts)
user0.reset_login_attempts()
print(user0.login_attempts)
# 9-7
from user import User
class Admin(User):
def __init__(self,first_name,last_name,sex,age):
super().__init__(first_name,last_name,sex,age)
self.privileges = 'can add post'
def show_privileges(self):
print(self.privileges)
admin = Admin('lily','green','female',20)
admin.show_privileges()
# 9-8
'''stroe class User in user.py '''
from user import User
class Privileges():
def __init__(self):
self.privileges = 'can add post'
def show_privileges(self):
print(self.privileges)
class Admin(User):
def __init__(self,first_name,last_name,sex,age):
super().__init__(first_name,last_name,sex,age)
self.privileges = Privileges()
admin = Admin('lily','green','female',20)
admin.privileges.show_privileges()