餐馆
1、第一步:创建一个名为Restaurant的类,其方法__init__()用于设置两个属性:restaurant_name 和 cuisine_type(烹饪);创建一个名为describe_restaurant()方法和一个名为open_restaurant ()方法,其中前者打印前述两项信息,而后者打印一条消息,指出餐馆正在营业;根据这个类创建一个名为restaurant的实例,分别打印其两个属性,再调用前述两个方法
2、第二步:根据第一步编写的类创建三个实例,并对每个实例调用方法 describe_restaurant()
3、第三步:添加一个名为number_served的属性,代表就餐人数,并将其默认值设置为0;打印有多少人在这家餐馆就餐,然后修改这个值并再次打印它;添加一个名为set_number_served()的方法,它让你能够设置就餐人数,调用这个方法并向它传递一个值,然后再次打印这个值;添加一个名为increment_number_served()的方法,它让你能够将就餐人数递增,调用这个方法并向它传递一个这样的值,用于表示餐厅又来了几名顾客,并显示当前就餐人数
class Restaurant(): def __init__(self,restaurant_name,cuisine_type,number_served = 0): #声明三个实例属性 self.restaurant_name = restaurant_name #餐馆名字 self.cuisine_type = cuisine_type #菜系 self.number_served = number_served #就餐人数 def describe_restaurant(self): print('名称:%s;菜系:%s'%(self.restaurant_name,self.cuisine_type)) def open_restaurant(self): print('欢迎光临%s,正在营业'%self.restaurant_name) #设置就餐人数 def set_number_served(self,n): self.number_served = n #通过传递的参数给实例属性赋值 print('当前就餐人数:%d'%self.number_served) #递增增加就餐人数 def increment_number_served(self,n): for i in range(1,n+1): self.number_served += 1 print('当前就餐人数:%d'%self.number_served) if __name__ == '__main__': restaurant = Restaurant('金拱门','西餐') #打印两个属性 print(restaurant.restaurant_name) #输出为金拱门 print(restaurant.cuisine_type) #输出为西餐 #调用两个方法 restaurant.describe_restaurant() #输出为 名称:金拱门;菜系:西餐 restaurant.open_restaurant() #输出为 欢迎光临金拱门,正在营业 chuancai = Restaurant('我家酸菜鱼', '川菜') chuancai.describe_restaurant() #输出为 名称:我家酸菜鱼;菜系:川菜 xiangcai = Restaurant('沪上湘城', '湘菜') xiangcai.describe_restaurant() #输出为 名称:沪上湘城;菜系:湘菜 loulan = Restaurant('楼兰', '新疆菜') loulan.describe_restaurant() #输出为 名称:楼兰;菜系:新疆菜 print('就餐人数:%d'%loulan.number_served) #输出为 就餐人数:0 loulan.number_served = 10 #通过对象名.属性名来设置属性值 print('就餐人数:%d' % loulan.number_served) #输出为 就餐人数:10 loulan.set_number_served(40) #输出为 当前就餐人数:40 loulan.increment_number_served(10) #输出为 当前就餐人数:50
用户
1、创建一个名为User的类,其中包含属性first_name和last_name,还有用户简介通常会存储的其他几个属性,在类User中定义一个名为describe_user()的方法,它打印用户信息摘要;再定义一个名为greet_user()的方法,它向用户发出个性化的问候;
2、尝试登录次数:在创建User类后,添加一个名为login_attempts的属性;编写一个名为increment_login_attempt()的方法,它将属性 login_attempts的值加 1;再编写一个名为reset_login_attempts()的方法,它将属性login_attempts的值重置为0;
3、根据User类创建一个实例,再调用方法increment_login_attempts()多次;打印属性login_attempts的值,确认它被正确地递增;然后,调用方法reset_login_attempts(),并再次打印属性login_attempts的值,确认它被重置为0
class User(): def __init__(self,first_name,last_name,age,sex,phone=0,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 %s,恭喜你中了五百万'%(self.first_name,self.last_name)) #增加登录次数 def increment_login_attempts(self): self.login_attempts += 1 print('当前登录次数%d'%self.login_attempts) #重置登录次数 def reset_login_attempts(self): self.login_attempts = 0 print('当前登录次数%d'%self.login_attempts) if __name__ == '__main__': joe = User('joe','black',19,'男','18600009999') joe.describe_user() #输出为 大家好我叫joe black,我今年19岁,我的电话是18600009999 joe.greet_user() #输出为 尊敬的joe black,恭喜你中了五百万 joe.increment_login_attempts() #输出为 当前登录次数1 joe.increment_login_attempts() #输出为 当前登录次数2 joe.increment_login_attempts() #输出为 当前登录次数3 joe.reset_login_attempts() #输出为 当前登录次数0
王者荣耀小游戏
1、创建游戏人物
属性:
名字:name,定位:category,血量:Output,技能:Skill
英雄:
铠,战士,血量:1000,技能:极刃风暴
2、游戏场景
偷红buff,释放技能偷到红buff消耗血量300
solo战斗,一血,消耗血量500
补血,加血200
class hero(): #定义属性 def __init__(self,name,category,skill,output=1000,score = 0): self.name = name self.category = category self.skill = skill self.output = output self.score = score #战斗场景1,偷红BUFF def red_buff(self): self.output -= 300 print('%s%s到对面野区偷红BUFF,消耗血量300'%(self.category,self.name)) #战斗场景2,solo战斗 def solo(self,n=1): self.output -= 500 if self.output < 0: print('%s%s,送了一个人头,血染王者峡谷'%(self.category,self.name)) else: if self.score == 0: self.score += n print('%s%s solo战斗拿到一血,消耗血量500'%(self.category,self.name)) else: self.score += n print('%s%s solo战斗收割%d个人头,消耗血量500'%(self.category,self.name,n)) #场景3,加血 def add_xue(self): self.output += 200 print('%s%s被辅助及时奶了一口,加血200'%(self.category,self.name)) #查看英雄相关信息 def getInfo(self): if self.output <= 0: print('%s%s,正在复活,拿到%d个人头'%(self.category,self.name,self.score)) else: print('%s%s血量还有%d,拿到%d个人头'%(self.category,self.name,self.output,self.score)) #实例化对象 kai = hero('铠','战士','极刃风暴') #操作 kai.red_buff() #输出为 战士铠到对面野区偷红BUFF,消耗血量300 kai.getInfo() #输出为 战士铠血量还有700,拿到0个人头 kai.solo() #输出为 战士铠 solo战斗拿到一血,消耗血量500 kai.getInfo() #输出为 战士铠血量还有200,拿到1个人头 kai.add_xue() #输出为 战士铠被辅助及时奶了一口,加血200 kai.getInfo() #输出为 战士铠血量还有400,拿到1个人头 kai.solo() #输出为 战士铠,送了一个人头,血染王者峡谷 kai.getInfo() #输出为 战士铠,正在复活,拿到1个人头
银行业务办理
类:
创建一个银行类
属性:
一个属于银行的类属性,用来存储所用银行的开户信息,包含卡号、密码、用户名、余额(外界不能随意访问和修改,开户时要进行卡号验证,查看卡号是否已经存在)
每个对象拥有卡号、密码、用户名、余额(外界不能随意访问和更改)
方法:
银行类可以查看本银行的开户总数,查看所有用户的个人信息(包含卡号、密码、用户名、余额)
每个对象可以实例化对象的时候传入相关参数,初始化对象及类属性
取钱(需要卡号和密码验证),通过验证卡号和密码对个人的余额进行操作,如果取钱大于余额,返回余额不足
存钱(需要卡号和密码验证),通过验证卡号和密码对个人的余额进行操作,返回操作成功
查看个人详细信息(需要卡号密码验证)
返回个人的卡号,用户名,余额信息
class Bank(): #一个属于银行的类属性 __Users = {} #每个对象拥有卡号、密码、用户名、余额 def __init__(self,CardId,pwd,name,balance): if CardId not in Bank.__Users: Bank.__Users[CardId] = {'pwd':pwd,'Username':name,'Balance':balance}#字典可以通过这种方式创建 self.__CardId = CardId self.__pwd = pwd self.__name = name self.__balance = balance #查看本银行的开户总数 @classmethod #只能操作类属性 def nums(cls): print('当前用户数:%d'%(len(cls.__Users))) #查看所有用户的个人信息(包含卡号、密码、用户名、余额) @classmethod def get_Users(cls): for key,val in cls.__Users.items(): print('卡号:%s \n用户名:%s \n密码:%d \n余额:%d'%(key,val['Username'],val['pwd'],val['Balance'])) print() #验证方法 @staticmethod def check_User(CardId,pwd): if (CardId in Bank.__Users) and (pwd == Bank.__Users[CardId]['pwd'] ): return True else: return False #验证金额 @staticmethod def check_money(money): if isinstance(money,int): return True else: return False #取钱(需要卡号和密码验证) def q_money(self,CardId,pwd,money): if Bank.check_User(CardId,pwd): #开始取钱 if Bank.check_money(money): if Bank.__Users[CardId]['Balance'] >= money: Bank.__Users[CardId]['Balance'] -= money print('当前卡号%s,取款金额为%d,当前余额%d'%(CardId,money,Bank.__Users[CardId]['Balance'])) else: print('余额不足') else: print('您输入的金额有误') else: print('卡号或者密码有误') #存钱(需要卡号和密码验证) def c_money(self,CardId,pwd,money): if Bank.check_User(CardId,pwd): #开始存钱 if Bank.check_money(money): Bank.__Users[CardId]['Balance'] += money print('当前卡号%s,当前存款金额%d,当前余额%d'%(CardId,money,Bank.__Users[CardId]['Balance'])) else: print('您输入的金额有误') else: print('卡号或者密码有误') #查看个人详细信息(需要卡号密码验证) def getInfo(self,CardId,pwd): if Bank.check_User(CardId, pwd): print('当前卡号%s,当前用户名%s,当前余额%d'%(CardId, Bank.__Users[CardId]['Username'], Bank.__Users[CardId]['Balance'])) else: print('卡号或者密码有误') 李相赫= Bank('1001',123456,'Faker',100) Bank.nums() #输出为 当前用户数:1 Bank.get_Users() #输出为 卡号:1001 用户名:Faker 密码:123456 余额:100 李相赫.c_money('1001',123456,500) #输出为 当前卡号1001,当前存款金额500,当前余额600 李相赫.q_money('1001',123456,300) #输出为 当前卡号1001,取款金额为300,当前余额300 李相赫.getInfo('1001',123456) #输出为 当前卡号1001,当前用户名Faker,当前余额300