xwb123

导航

第七章

实例01--创建大雁类并定义飞行方法

class Geese:
    '''大雁类'''
    def __init__(self,beak,wing,claw):
        print('我是大雁类!我有以下特征:')
        print(beak)
        print(wing)
        print(claw)
    def fly(self,state):
        print(state)
beak_1="喙的基部比较高,长度和头部的长度几乎相等"
wing_1="翅膀长而尖"
claw_1="爪子是蹼状的"
wildGoose=Geese(beak_1,wing_1,claw_1)
wildGoose.fly('我飞行的时候,一会排成个人字,一会排成个一字')

 

 

 

 实例02--通过类属性统计类的实例个数

class Geese:
    '''雁类'''
    neck='脖子比较长'
    wing='翅膀频率高'
    leg='腿位于身体的中心支点,行走自如'
    number=0
    def __init__(self):
        Geese.number+=1
        print('\n我是第'+str(Geese.number)+'只大雁,我属于大雁类!我有以下特征:')
        print(Geese.neck)
        print(Geese.wing)
        print(Geese.leg)
list1=[]
for i in range(4):
    list1.append(Geese())
print('一共有'+str(Geese.number)+'只大雁')

 

 

 实例03--在模拟电影点播功能时应用属性

class TVshow:
    list_film=['战狼','红海行动','西游记女儿国','熊出没·变形记']
    def __init__(self,show):
        self.__show=show
    @property
    def show(self):
        return self.__show
    @show.setter
    def show(self,value):
        if value in TVshow.list_film:
            self.__show='您选择了《'+value+'》,稍后将播放'
        else:
            self.__show='您点播的电影不存在'
tvshow=TVshow('战狼')
print('正在播放:《',tvshow.show,'')
print('您可以从',tvshow.list_film,'中选择要点播的电影')
tvshow.show='红海行动'
print(tvshow.show)
    

 

 

 

实例04--创建水果基类及其派生类

class Fruit:
    color='绿色'
    def harvest(self,color):
        print('水果是:'+color+'的!')
        print('水果已经收获······')
        print('水果原来是:'+Fruit.color+'的!')
class Apple(Fruit):
    color='红色'
    def __init__(self):
        print('我是苹果')
class Orange(Fruit):
    color='橙色'
    def __init__(self):
        print('\n我是橘子')
apple=Apple()
apple.harvest(apple.color)
orange=Orange()
orange.harvest(orange.color)

 

 实例05--在派生类中调用基类的__init__()方法定义类的属性

class Fruit:
    def __init__(self,color='绿色'):
        Fruit.color=color
    def harvest(self,color):
        print('水果是:'+self.color+'的!')
        print('水果已经收获······')
        print('水果原来是:'+Fruit.color+'的!')
class Apple(Fruit):
    color='红色'
    def __init__(self):
        print('我是苹果')
        super().__init__()
class Sapodilla(Fruit):
    def __init__(self,color):
        print('\n我是人参果')
        super().__init__(color)
    def harvest(self,color):
        print('人参果是:'+color+'的!')
        print('人参果已经收获······')
        print('人参果原来是:'+Fruit.color+'的!')
apple=Apple()
apple.harvest(apple.color)
sapodilla=Sapodilla('白色')
sapodilla.harvest('金黄色带紫色条纹')

 

 

实战一:修改手机默认语言

class phone:   #定义手机类
    '''手机类'''
    def __init__(self,language='英文'):  #构造方法
        if language=='英文':
            print('智能手机的默认语言为'+language)
        else:
            print('将智能手机的默认语言设置为'+language)
phone()   #无参数
phone('中文')  #有参数

 

 

实战二:给信用卡设置默认密码

class Card:
    '''信用卡类'''
    def __init__(self,number,code='123456'):
        if code=='123456':
            print('信用卡'+str(number)+'的默认密码为'+str(code))
        else:
            print('重置信用卡'+str(number)+'的密码为'+str(code))
Card('4013735633800642')
Card('4013735633800642','168779')

 

 

 实战三:打印每月销售明细

class SaleHandler:
    '''销售管理类'''
    def __init__(self):
        self.__sale_data = {              
             "2":[('T0001','笔记本电脑'),
                  ('T0002','荣耀6X'),
                  ('T0003','iPad'),
                  ('T0004','荣耀V9'),
                  ('T0005','MACBook')]}   #{key:value}
    def querySaleList(self,query_month):
        '''根据输入月份,查询商品明细'''
        if query_month in self.__sale_data:
            print("%s月份商品销售明细如下:" % query_month)
            for item in self.__sale_data[query_month]:
                print("商品编号:%s 商品名称:%s" % item)
        else:
            print("该月份没有销售数据或输入月份有误!")
print("------销售明细-----")
sh = SaleHandler()
while True:
    month = input("\n请输入要查询的月份(例如1、2、3等):")
    if month == 'quit':
        break 
    
    sh.querySaleList(month)

 

 

 实战四:模拟电影院售票机

class Film:
    """
    电影类
    有三个属性:电影名字 播放场次列表 剩余座位列表
    """
    def __init__(self,film_name,timelist,seatlist):  #构造方法
        self.name=film_name
        self.timeList=timelist
        self.seatList=seatlist

class Ticket:
    def __init__(self,film,time,seat):
        self.__film=film
        self.__time=time
        self.__seat=seat

    def __str__(self):
        return'电影:'+self.__film+'\n放映时间:'+self.__time+'\n座位:'+self.__seat

class TicketMachine:
    def __init__(self):
        self.__filmList=[Film('《长津湖》',['9:30','10:40','12:00'],['10-01','10-02','10-04']),
                         Film('《战狼2》',['8:30','9:40','11:00'],['2-01','2-02']),
                         Fllm('《红海行动》',['13:30','15:40','20:00','21:15'],['5-01','5-02','5-10','5-12'])]
        print('欢迎使用自动贩卖机~~')
    def takeTicket(self):
        print('\n请选择正在上映的电影:',end='')
        for index,item in enumerate(self.__filmList):
            print(index+1,item.name,sep='.',end='')
        print()

        inputselect='2'  #假设用户输入选择为2
        sfilm=self.__filmList[int(inputselect)-1]
        selectfilm=sfilm.name
        print('已选电影:'+selectfilm)

        print('\n请选择电影播放场次:',end='') #遍历选择的电影时间列表,显示全部可选择时间
        for index,item in enumerate(sfilm.timeList):
            print(index+1,item,sep='.',end='')
        print()
        inputselect='2'  #假设选择了第2个10:40场次
        selecttime=sfilm.timeList[int(inputselect)-1]  #获取选择时间
        print('电影场次:'+selecttime)

        print('\n请选择座位:',end='') #遍历以选择电影剩余座位,输出全部可选择座位
        for index,item in enumerate(sfilm.seatlist):
            print(index+1,item,sep='.',end='')
        print()
        inputselect='2'  #假设选择了第2个10-02
        selectseat=sfilm.seatList[int(inputselect)-1]  #获取座位号
        print('选择座位:'+selectseat)

        #模拟出票
        print('\n正在出票......\n')
        tk=Film(selectfilm,selecttime,selectseat)
        #输出电影票信息,输出内容的格式是电影票中的str定义的
        print(tk)
        print('\n出票完成,请别忘记取票!')

            
 

 

posted on 2022-12-14 19:55  雪人头子  阅读(17)  评论(0编辑  收藏  举报