day18作业(1)

  1. 建立一个汽车类Auto,包括轮胎个数,汽车颜色,车身重量,速度等属性,并通过不同的构造方法创建实例。至少要求 汽车能够加速 减速 停车。 再定义一个小汽车类CarAuto 继承Auto 并添加空调、CD属性,并且重新实现方法覆盖加速、减速的方法

    class Auto:
        __max = 200
    
        def __init__(self, tyre_count=4, color='白色', weight=1.5, speed=0):
            self.tyre_count = tyre_count
            self.color = color
            self.weight = weight
            self.speed = speed
    
        def add_speed(self, value):
            if self.speed + value > Auto.__max:
                self.speed = Auto.__max
            else:
                self.speed += value
    
        def sub_speed(self, value):
            if self.speed - value < 0:
                self.speed = 0
            else:
                self.speed -= value
    
        def stop(self):
            self.speed = 0
    
    
    class AirConditioner:
        def __init__(self, brand, price):
            self.brand = brand
            self.price = price
    
    
    class CD:
        def __init__(self):
            pass
    
    
    class CarAuto(Auto):
        def __init__(self, tyre_count=4, color='白色', weight=1.5, speed=0):
            super().__init__(tyre_count, color, weight, speed)
            self.air_conditioner = AirConditioner('芝东', 2000)
            self.cd = CD()
    
        pass
    
    
  2. 创建一个Person类,添加一个类字段用来统计Perosn类的对象的个数

    class Person:
        count = 0
    
        def __init__(self):
            Person.count += 1
    
    
    print(Person.count)
    
    p1 = Person()
    print(Person.count)
    p2 = Person()
    p3 = Person()
    print(Person.count)
    
  3. 创建一个动物类,拥有属性:性别、年龄、颜色、类型 ,

    要求打印这个类的对象的时候以’/XXX的对象: 性别-? 年龄-? 颜色-? 类型-?/’ 的形式来打印

    class Animal:
        def __init__(self, sex='雄', age=3, color='灰色', type='爬行'):
            self.sex = sex
            self.age = age
            self.color = color
            self.type = type
    
        def __repr__(self):
            return f'/{self.__class__.__name__}的对象:性别-{self.sex} 年龄-{self.age} 颜色-{self.color} 类型-{self.type}/'
            # return f'/{Animal.__name__}的对象:性别-{self.sex} 年龄-{self.age} 颜色-{self.color} 类型-{self.type}/'
    
    
    class Cat(Animal):
        pass
    
    
    a = Animal()
    print(a)
    
    c = Cat()
    print(c)
    
    
  4. (不写)写一个圆类, 拥有属性半径、面积和周长;要求获取面积和周长的时候的时候可以根据半径的值把对应的值取到。但是给面积和周长赋值的时候,程序直接崩溃,并且提示改属性不能赋值

  5. 写一个扑克类, 要求拥有发牌和洗牌的功能(具体的属性和其他功能自己根据实际情况发挥)

  6. (尝试)写一个类,其功能是:1.解析指定的歌词文件的内容 2.按时间显示歌词 提示:歌词文件的内容一般是按下面的格式进行存储的。歌词前面对应的是时间,在对应的时间点可以显示对应的歌词

    [00:00.20]蓝莲花   
    [00:00.80]没有什么能够阻挡   
    [00:06.53]你对自由地向往   
    [00:11.59]天马行空的生涯  
    [00:16.53]你的心了无牵挂   
    [02:11.27][01:50.22][00:21.95]穿过幽暗地岁月   
    [02:16.51][01:55.46][00:26.83]也曾感到彷徨   
    [02:21.81][02:00.60][00:32.30]当你低头地瞬间  
    [02:26.79][02:05.72][00:37.16]才发觉脚下的路   
    [02:32.17][00:42.69]心中那自由地世界  
    [02:37.20][00:47.58]如此的清澈高远   
    [02:42.32][00:52.72]盛开着永不凋零   
    [02:47.83][00:57.47]蓝莲花  
    class Lyric:
     def __init__(self, word: str, time: str):
            self.word = word
    
            m = int(time[1:3])
         s = float(time[4:])
            self.time = m * 60 + s
    
        def __repr__(self):
            return f'{self.time}: {self.word}'
    
        def __lt__(self, other):
            return self.time < other.time
    
    
    class LyricResolver:
    
        def __init__(self, file_path):
            self.file_path = file_path
            self.all_lyric = []
    
        def __deal_line_world(self, line):
            if not match(r'\[\d\d:\d\d\.\d\d\]', line):
                return
            line_list = line.split(']')
            word = line_list[-1]
            for t in line_list[:-1]:
                ly = Lyric(word, t)
                self.all_lyric.append(ly)
    
        def __analysis_lyric_file(self):
            """解析歌词文件"""
            try:
                with open(self.file_path, 'r', encoding='utf-8') as f:
                    while True:
                        line_content = f.readline()
                        if not line_content:
                            break
                        # 处理一行歌词
                        # print(line_content)
                        self.__deal_line_world(line_content)
                # 歌词按时间排序
                # self.all_lyric.sort(key=lambda item: item.time)
                self.all_lyric.sort(reverse=True)
                print(self.all_lyric)
            except FileNotFoundError:
                print('当前的歌词文件不存在!')
    
        def get_word(self, time):
            """获取歌词"""
            if not self.all_lyric:
                self.__analysis_lyric_file()
                
            for l in self.all_lyric:
                if l.time < time:
                    return l.word
    
    
    l1 = LyricResolver('files/蓝莲花.txt')
    print('==============')
    print(l1.get_word(10))
    print(l1.get_word(20))
    
    l2 = LyricResolver('files/我很喜欢.txt')
    
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值