文章目录
- 示例1:创建和使用类
- 示例2:创建和使用类
- 示例3:继承示例1
- 示例4:继承示例2
- 示例5:继承,将实例用作属性
示例1:创建和使用类
创建一个名为Restaurant 的类,其方法__ init __()
设置两个属性:restaurant_name 和cuisine_type 。创建一个名为describe_restaurant() 的方法和一个名为open_restaurant() 的方法,其中前者打印前述两项信息,而后者打印一条消息,指出餐馆正在营业。根据这个类创建一个名为restaurant 的实例,分别打印其两个属性,再调用前述两个方法。
再向程序中,添加一个名为number_served 的属性,并将其默认值设置为0。根据这个类创建一个名为restaurant 的实例;打印有多少人在这家餐馆就餐过,然后修改这个值并再次打印它。
添加一个名为set_number_served() 的方法,它让你能够设置就餐人数。调用这个方法并向它传递一个值,然后再次打印这个值。
添加一个名为increment_number_served() 的方法,它让你能够将就餐人数递增。调用这个方法并向它传递一个这样的值:你认为这家餐馆每天可能接待的就餐人数。
class Restaurant():
"""创建一个打印餐馆的信息"""
def __init__(self,name,type):
self.name = name
self.type = type
self.number_served = 0
def describe_restaurant(self):
print('\nRestaurant Name:'+ self.name.title())
print('\nCuisine Type:'+ self.type.title())
def open_restaurant(self):
print('\n'+self.name.title()+' is opening!')
def read_number(self):
print("\nNumber of People Eating:"+str(self.number_served))
def set_number_served(self,num):
self.number_served = num
def increment_served_number(self,increment_number):
self.number_served += increment_number
mr = Restaurant('和平饭店','四大菜系')
#print(mr.name.title())
mr.describe_restaurant()
mr.open_restaurant()
#mr.number_served=10
mr.set_number_served(500)
mr.increment_served_number(400)
mr.read_number()
#mr1 = Restaurant('water restaurant','chuan cai')
#mr1.describe_restaurant()
'''
Restaurant Name:和平饭店
Cuisine Type:四大菜系
和平饭店 is opening!
Number of People Eating:900
'''
示例2:创建和使用类
创建一个名为User
的类,其中包含属性first_name 和last_name ,还有用户简介通常会存储的其他几个属性。在类User 中定义一个名为describe_user()
的方法,它打印用户信息摘要;再定义一个名为greet_user()
的方法,它向用户发出个性化的问候。创建多个表示不同用户的实例,并对每个实例都调用上述两个方法。
在为完成编写的User 类中,添加一个名为login_attempts 的属性。编写一个名为increment_login_attempts()
的方法,它将属性login_attempts 的值加1。再编写一个名为reset_login_attempts() 的方法,它将属性login_attempts 的值重置为0。根据User 类创建一个实例,再调用方法increment_login_attempts() 多次。打印属性login_attempts 的值,确认它被正确地递增;然后,调用方
法reset_login_attempts() ,并再次打印属性login_attempts 的值,确认它被重置为0。
class User():
"""存储并打印用户信息"""
def __init__(self,first_name,last_name,age,location,school,education):
self.first_name = first_name.title()
self.last_name = last_name.title()
self.age = age
self.location = location.title()
self.education = education.title()
self.login_attempts = 0
def describe_user(self):
print('\nFirst Name:'+self.first_name)
print('Last Name:'+self.last_name)
print('Age:'+str(self.age))
print('Location:'+self.location)
print('Education:'+self.education)
def greet_user(self):
print("Hello,"+self.last_name+' '+self.first_name)
def increment_login_attempts(self):
self.login_attempts += 1
def reset_login_attempts(self):
self.login_attempts = 0
def read_login_attempts(self):
print("\nLogin Times:"+str(self.login_attempts))
me = User('erxiao','wang',22,'beijing','BUAA','master')
me.describe_user()
me.greet_user()
for i in range(30):
me.increment_login_attempts()
me.read_login_attempts()
me.reset_login_attempts()
me.read_login_attempts()
'''
First Name:Erxiao
Last Name:Wang
Age:22
Location:Beijing
Education:Master
Hello,Wang Erxiao
Login Times:30
Login Times:0
'''
示例3:继承示例1
冰淇淋小店是一种特殊的餐馆。编写一个名为IceCreamStand 的类,让它继承示例1编写的Restaurant 类。添加一个名为flavors 的属性,用于存储一个由各种口味的冰淇淋组成的列表。编写一个显示这些冰淇淋的方法。创建一个IceCreamStand 实例,并调用这个方法。
class Restaurant():
"""创建一个打印餐馆的信息"""
def __init__(self,name,type):
self.name = name
self.type = type
self.number_served = 0
def describe_restaurant(self):
print('\nRestaurant Name:'+self.name.title())
print('\nCuisine Type:'+self.type.title())
def open_restaurant(self):
print('\n'+self.name.title()+' is opening!')
def read_number(self):
print("\nNumber of People Eating:"+str(self.number_served))
def set_number_served(self,num):
self.number_served = num
def increment_served_number(self,increment_number):
self.number_served += increment_number
class IceCreamStand(Restaurant):
def __init__(self,name,type):
super().__init__(name,type)
self.flavors=['A','B','C']
def display(self):
for flavor in self.flavors:
print(flavor)
mr = IceCreamStand('北京饭店','鲁菜')
#print(mr.name.title())
mr.describe_restaurant()
mr.open_restaurant()
#mr.number_served=10
mr.set_number_served(500)
mr.increment_served_number(400)
mr.read_number()
mr.display()
#mr1 = Restaurant('water restaurant','chuan cai')
#mr1.describe_restaurant()
'''
Restaurant Name:北京饭店
Cuisine Type:鲁菜
北京饭店 is opening!
Number of People Eating:900
A
B
C
'''
示例4:继承示例2
管理员是一种特殊的用户。编写一个名为Admin 的类,让它继承示例2编写的User 类。添加一个名为privileges 的属性,用于存储一个由字符串(如"can add post" 、“can delete post” 、“can ban user” 等)组成的列表。编写一个名为show_privileges() 的方法,它显示管理员的权限。创建一个Admin 实例,并调用这个方法。
class User():
"""存储并打印用户信息"""
def __init__(self,first_name,last_name,age,location,school,education):
self.first_name = first_name.title()
self.last_name = last_name.title()
self.age = age
self.location = location.title()
self.education = education.title()
self.login_attempts = 0
def describe_user(self):
print('\nFirst Name:'+self.first_name)
print('Last Name:'+self.last_name)
print('Age:'+str(self.age))
print('Location:'+self.location)
print('Education:'+self.education)
def greet_user(self):
print("Hello,"+self.last_name+' '+self.first_name)
def increment_login_attempts(self):
self.login_attempts += 1
def reset_login_attempts(self):
self.login_attempts = 0
def read_login_attempts(self):
print("\nLogin Times:"+str(self.login_attempts))
class Admin(User):
def __init__(self,first_name,last_name,age,location,school,education):
super().__init__(first_name,last_name,age,location,school,education)
self.privileges = ["can add post","can delete post","can ban user"]
def show_privileges(self):
for value in self.privileges:
print(value.title())
me = Admin('erxiao','wang',22,'beijing','BUAA','master')
me.describe_user()
me.greet_user()
for i in range(30):
me.increment_login_attempts()
me.read_login_attempts()
me.reset_login_attempts()
me.read_login_attempts()
me.show_privileges()
'''
First Name:Erxiao
Last Name:Wang
Age:22
Location:Beijing
Education:Master
Hello,Wang Erxiao
Login Times:30
Login Times:0
Can Add Post
Can Delete Post
Can Ban User
'''
示例5:继承,将实例用作属性
编写一个名为Privileges
的类,它只有一个属性——privileges ,其中存储了示例4所说的字符串列表。将方法show_privileges() 移到这个类中。在Admin 类中,将一个Privileges 实例用作其属性。创建一个Admin 实例,并使用方法show_privileges() 来显示其权限。
class User():
"""存储并打印用户信息"""
def __init__(self,first_name,last_name,age,location,school,education):
self.first_name = first_name.title()
self.last_name = last_name.title()
self.age = age
self.location = location.title()
self.education = education.title()
self.login_attempts = 0
def describe_user(self):
print('\nFirst Name:'+self.first_name)
print('Last Name:'+self.last_name)
print('Age:'+str(self.age))
print('Location:'+self.location)
print('Education:'+self.education)
def greet_user(self):
print("Hello,"+self.last_name+' '+self.first_name)
def increment_login_attempts(self):
self.login_attempts += 1
def reset_login_attempts(self):
self.login_attempts = 0
def read_login_attempts(self):
print("\nLogin Times:"+str(self.login_attempts))
class Privileges():
def __init__(self):
self.privileges= ["can add post","can delete post","can ban user"]
def show_privileges(self):
for value in self.privileges:
print(value.title())
class Admin(User):
def __init__(self,first_name,last_name,age,location,school,education):
super().__init__(first_name,last_name,age,location,school,education)
self.privileges = Privileges()
me = Admin('erxiao','wang',22,'beijing','BUAA','master')
me.describe_user()
me.greet_user()
for i in range(30):
me.increment_login_attempts()
me.read_login_attempts()
me.reset_login_attempts()
me.read_login_attempts()
me.privileges.show_privileges()
'''
First Name:Erxiao
Last Name:Wang
Age:22
Location:Beijing
Education:Master
Hello,Wang Erxiao
Login Times:30
Login Times:0
Can Add Post
Can Delete Post
Can Ban User
'''