python 学习day 7
2019-09-11 10:57 叫我永康就好啦 阅读(159) 评论(0) 收藏 举报1、代码学习部分
class Restaurant():
def __init__(self,restaurant_name,cuisine_name):
self.restaurant_name=restaurant_name #第一个restaurant_name是定义的变量 第二个是形参
self.cuisine_name=cuisine_name
def describe_restaurant(self):
print("The name of restaurant is "+self.restaurant_name, "Food type is "+self.cuisine_name)
def open_restaurant(self):
print("The restaurant is open.")
restaurant=Restaurant('zhongcanting','Chinese food')
restaurant.describe_restaurant()
restaurant.open_restaurant()
class Restaurant():
def __init__(self,restaurant_name,cuisine_name):
self.restaurant_name=restaurant_name
self.cuisine_name=cuisine_name
def describe_restaurant(self):
print("The name of restaurant is "+self.restaurant_name, "Food type is "+self.cuisine_name)
def open_restaurant(self):
print("The restaurant is open.")
restaurant1=Restaurant('zhongcanting','Chinese food')
restaurant2=Restaurant('western restaurant','western food')
restaurant3=Restaurant('Michelin','western food')
restaurant1.describe_restaurant()
restaurant2.describe_restaurant()
restaurant3.describe_restaurant()
class User():
def __init__(self,first_name,last_name):
self.first_name=first_name
self.last_name=last_name
def describe_user(self):
print("Your full name is "+self.first_name+self.last_name)
def greet_user(self):
print("Good morning, "+self.first_name+self.last_name)
user=User('zhang','yongkang')
user.describe_user()
user.greet_user()
#9.2节 使用类和实例
class Restaurant():
def __init__(self,restaurant_name,cuisine_name):
self.restaurant_name=restaurant_name #第一个restaurant_name是定义的变量 第二个是形参
self.cuisine_name=cuisine_name
self.number_served=0
def describe_restaurant(self):
print("The name of restaurant is "+self.restaurant_name, "Food type is "+self.cuisine_name)
def open_restaurant(self):
print("The restaurant is open.")
def set_number_served(self,number_updated):
self.number_served=number_updated
def increment_number_served(self,increment):
self.number_served+=increment
restaurant=Restaurant('zhongcanting','Chinese food')
print("The number served in the restautant is "+str(restaurant.number_served)+".")
restaurant.number_served=3
print("The number served in the restautant is "+str(restaurant.number_served)+".")
restaurant.set_number_served(6)
print("The number served in the restautant is "+str(restaurant.number_served)+".")
restaurant.increment_number_served(10)
print("The number served in the restautant is "+str(restaurant.number_served)+".")
class User():
def __init__(self,first_name,last_name):
self.first_name=first_name
self.last_name=last_name
self.login_attempts=0
def describe_user(self):
print("Your full name is "+self.first_name+self.last_name)
def greet_user(self):
print("Good morning, "+self.first_name+self.last_name)
def increment_login_attempts(self):
self.login_attempts+=1
def reset_login_attempts(self):
self.login_attempts=0
user=User('zhang','yongkang') #创建实例
user.increment_login_attempts()
user.increment_login_attempts()
user.increment_login_attempts()
user.increment_login_attempts()
print("The number of login_attempt is "+str(user.login_attempts))
user.reset_login_attempts()
print("The number of login_attempt is "+str(user.login_attempts))
#继承
class Restaurant():
def __init__(self,restaurant_name,cuisine_name,icecream_name):
self.restaurant_name=restaurant_name #第一个restaurant_name是定义的变量 第二个是形参
self.cuisine_name=cuisine_name
self.number_served=0
self.flavors=icecream_name
def describe_restaurant(self):
print("The name of restaurant is "+self.restaurant_name, "Food type is "+self.cuisine_name)
def open_restaurant(self):
print("The restaurant is open.")
def set_number_served(self,number_updated):
self.number_served=number_updated
def increment_number_served(self,increment):
self.number_served+=increment
def describe_icecream(self):
print(self.flavors)
class IceCreamStand(Restaurant):
def __init__(self,restaurant_name,cuisine_name,icecream_name):
super().__init__(restaurant_name,cuisine_name,icecream_name)
printed_icecream=IceCreamStand('zhongcanting','Chinese food','shengdai')
print(printed_icecream.flavors)
class User():
def __init__(self,first_name,last_name):
self.first_name=first_name
self.last_name=last_name
self.login_attempts=0
self.privileges = ['can add post', 'can delete post', 'can ban user']
def describe_user(self):
print("Your full name is "+self.first_name+self.last_name)
def greet_user(self):
print("Good morning, "+self.first_name+self.last_name)
def increment_login_attempts(self):
self.login_attempts+=1
def reset_login_attempts(self):
self.login_attempts=0
def show_Privileges(self):
print(self.privileges)
class Admin(User):
def __init__(self,first_name,last_name):
super().__init__(first_name,last_name)
privileges_show=Admin('zhang','yongkang')
privileges_show.show_Privileges()
#9-8 权限
class Privileges():
def __init__(self):
self.Privileges=['can add post', 'can delete post', 'can ban user']
def show_Privileges(self):
print(self.privileges)
class Admin(User):
def __init__(self,first_name,last_name):
super().__init__(first_name,last_name)
self.showPrivileges=Privileges() #实例用作属性
privileges_show=Admin('zhang','yongkang')
privileges_show.show_Privileges()
#9-9电瓶升级
#略
#9.4 导入类
from restaurant import Restaurant
restaurant=Restaurant('zhongcanting','Chinese food','shengdai')
restaurant.describe_restaurant()
from users import User,Admin,Privileges
privileges_show=Admin('zhang','yongkang')
privileges_show.show_Privileges()
2、学习疑问与心得
2.1、在Python中,首字母大写的名称指的是类,模块名誉实例名都用小写
2.2、类中的函数称为方法
2.3、创建实例:my_dog=Dog('While',6) 通产可以认为首字母大写的名称是类 而小写的名称(例如my_dog)是根据类 创建的实例
2.4、创建子类时,父类必须包含在当前文件中
2.5、对于每个类,都应紧跟在类定义后面包含一个文档字符串,(即三个引号圈起来的那段文字)简短地描述类的功能。每个模块也都应包含一个文档字符串
浙公网安备 33010602011771号