day15-类和对象作业

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

  def add_speed(self, value):
      self.speed += value
      if self.speed >= 200:
          self.speed = 200

  def sub_speed(self, value):
      self.speed -= value
      if self.speed < 0:
          self.speed = 0

  def stop(self):
      self.speed = 0


class CarAuto(Auto):
  def __init__(self, color, weight, air_conditioner, speed=0, tire=4, cd=''):
      super().__init__(color, weight, speed, tire)
      self.cd = cd
      self.air_conditioner = air_conditioner

  def add_speed(self, value):
      self.speed += value
      if self.speed >= 300:
          self.speed = 300

  def sub_speed(self, value):
      self.speed -= value
      if self.speed < 0:
          self.speed = 0


# 2.创建一个Person类,添加一个类字段用来统计Perosn类的对象的个数
class Person:
  count = 0

  def __init__(self, name='', age=0):
      self.name = name
      self.age = age
      Person.count += 1


p1 = Person()
p2 = p1

p3 = Person()
print(Person.count)
  1. 创建一个Person类,添加一个类字段用来统计Perosn类的对象的个数

    class Person:
        num = 0
    
        def __init__(self, name):
            self.name =name
            Person.num += 1
    
    
    p1 = Person('阿岛')
    p2 = Person('阿党')
    print(Person.num)
    
  2. 创建一个动物类,拥有属性:性别、年龄、颜色、类型 ,

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

    class Animal:
        def __init__(self, gender, age, color, type1):
            self.gender = gender
            self.age = age
            self.color = color
            self.type1 = type1
    
     @staticmethod
        def output(self, class_dict):
            class_name = Animal.__name__
            output_string = class_name + '的对象:'
            for key in class_dict:
                output_string += key + '-' + str(class_dict[key]) + '  '
            print(output_string)
    
    
    A1 = Animal('母', 3, 'red', '猫')
    A1.output(A1.__dict__)
    
  3. 写一个圆类, 拥有属性半径、面积和周长;要求获取面积和周长的时候的时候可以根据半径的值把对应的值取到。但是给面积和周长赋值的时候,程序直接崩溃,并且提示改属性不能赋值

    class ReadError(Exception):
        def __str__(self):
            return '属性不能赋值!'
    
    
    class Circle:
        __pai = 3.14
    
        def __init__(self, r):
            self.r = r
            self.__area = 0
            self.__perimeter = 0
    
        @property
        def area(self):
            return Circle.__pai * self.r ** 2
    
        @area.setter
        def area(self, value):
            raise ReadError
    
        @property
        def perimeter(self):
            return 2 * Circle.__pai * self.r
    
        @perimeter.setter
        def perimeter(self, value):
            raise ReadError
    
    
    c1 = Circle(3)
    print(c1.area)
    print(c1.perimeter)
    # c1.perimeter = 23
    # c1.area = 4
    
  4. (尝试)写一个类,其功能是: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):
        self._time = 0
        self.word = word

    @property
    def time(self):
        return self._time

    @time.setter
    def time(self, value):
        fen = float(value[1:3])
        miao = float(value[4:])
        self._time = fen*60+miao

    def __repr__(self):
        return '<%s>' % str(self.__dict__)[1:-1]

    def __lt__(self, other):
        return self.time < other.time


class LyricResolver:
    """歌词解析器类,主要提供歌词解析相关方法"""
    def __init__(self, name: str):
        self.name = name
        self.all_Lyric = []

    def get_lyric(self, time1):
        if not self.all_Lyric:
            print('================')
            # 1. 读文件中的内容
            with open('files/'+self.name, 'r', encoding='utf-8') as f:
                while True:
                    line = f.readline()
                    if not line:
                        break
                    # 将时间和歌词分开
                    lines = line.split(']')
                    # 将时间和歌词一一对应
                    # print(lines)
                    word = lines[-1]    # 时间对应的歌词
                    for time in lines[0:-1]:
                        # 创建歌词对象
                        ly = Lyric(word)
                        ly.time = time
                        # 保存歌词对象
                        self.all_Lyric.append(ly)
            # 2. 按时间对所有的歌词进行排序
            self.all_Lyric.sort(reverse=True)
            # print(self.all_Lyric)

        # 3.根据时间找歌词
        for ly in self.all_Lyric:
            if ly.time <= time1:
                return ly.word


l1 = LyricResolver('蓝莲花.lrc')
print(l1.get_lyric(100))

print(l1.get_lyric(120))


l2 = LyricResolver('慢自己')
print(l2.get_lyric(20))

print(l1.get_lyric(40))

你可能感兴趣的:(day15-类和对象作业)