day15

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

        def speed_up(self, value):
            self.speed += value
            return "这车以%s每秒加速,现在的速度为%s" % (value, self.speed)
    
        def slow_down(self, value):
            self.speed -= value
            return "这车以%s每秒减速,现在的速度为%s" % (value, self.speed)
    
        def stop(self):
            return "这辆%s的%s已经停啦" % (self.color, self.name)
    
    
     class CarAuto(Auto):
         def __init__(self, air_condition, CD):
             self.air_condition = air_condition
             self.CD = CD
    
         def speed_up(self, value):
             super().__init__('黑色', 120, 80, '宝马')
             self.speed += value
             return "这辆%s的%s以%s每秒加速,现在的速度为%s" % (self.color, self.name, value, self.speed)
    
         def slow_down(self, value):
             super().__init__('黑色', 120, 80, '宝马')
             self.speed -= value
             return "这辆%s的%s以%s每秒加速,现在的速度为%s" % (self.color, self.name, value, self.speed)
    
    
     p1 = Auto('红色', 180, 100, '保时捷')
     print(p1.speed_up(50))
     print(p1.slow_down(50))
     print(p1.stop())
    
     p2 = CarAuto('美的', '黑色星期五')
     print(p2.speed_up(30))
     print(p2.slow_down(30))
    
  2. 创建一个Person类,添加一个类字段用来统计Perosn类的对象的个数
    class Person(object):
    count = 0

        def __init__(self, name, ID, class_id, gender, weight, height):
            self.name = name
            self.ID = ID
            self.class_id = class_id
            self.gender = gender
            self.weight = weight
            self.height = height
    
        @classmethod
        def sum1(cls):
            dict1 = dict(p1.__dict__)
            for _ in dict1:
                cls.count += 1
            return cls.count
    
    
    p1 = Person('刘德华', 123456, '一班', '男', 160, 180)
    print(p1.sum1())
    
  3. 创建一个动物类,拥有属性:性别、年龄、颜色、类型 ,
    要求打印这个类的对象的时候以'/XXX的对象: 性别-? 年龄-? 颜色-? 类型-?/' 的形式来打印
    class Animal(object):
    def init(self, gender, age, color, type1):
    self.gender = gender
    self.age = age
    self.color = color
    self.type1 = type1

        def print_animal(self):
            return "%s的对象: 性别-%s,年龄-%s,颜色-%s,类型-%s" % (Animal.__name__, self.gender, self. age, self.color, self.type1)
    
    
    p1 = Animal('男', 2, '红色', '地上跑的')
    print(p1.print_animal())
    
  4. 写一个圆类, 拥有属性半径、面积和周长;要求获取面积和周长的时候的时候可以根据半径的值把对应的值取到。但是给面积和周长赋值的时候,程序直接崩溃,并且提示改属性不能赋值
    import math

   class WriteError(Exception):
       def __str__(self):
           return '只读属性不能赋值!'
   
   
   class Circle(object):
       def __init__(self, radius):
           self.radius = radius
           self._perimeter = math.pi * 2 * radius
           self._area = math.pi * (radius ** 2)
   
       @property
       def perimeter(self):
           return self._perimeter
   
       @perimeter.setter
       def perimeter(self, values):
           raise WriteError
   
       @property
       def area(self):
           return self._area
   
       @area.setter
       def area(self, values):
           raise WriteError
   
   
   p1 = Circle(5)
   print(p1.area, p1.perimeter)
   
   # p1.area = 100
   # p1.perimeter = 100
  1. 写一个扑克类, 要求拥有发牌和洗牌的功能(具体的属性和其他功能自己根据实际情况发挥)
    import random
   class Poker(object):
       list1 = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']
       list2 = ['黑桃', '红桃', '方块', '梅花']
       card = []
       for col in list2:
           for num in list1:
               card.append(col + num)
       card.append('大王')
       card.append('小王')
   
       @classmethod
       def all_poker(cls):
           return cls.card
   
       def shuffle(self):
           Poker.card = list(Poker.card)
           random.shuffle(Poker.card)
           return Poker.card
   
       def licensing(self):
   
           player1 = Poker.card[0:17]
           player2 = Poker.card[17:34]
           player3 = Poker.card[34:51]
           last = Poker.card[51:]
           return player1, player2, player3, last
   
   
   p1 = Poker()
   print(p1.all_poker())
   p1.shuffle()
   print(p1.licensing())
  1. (尝试)写一个类,其功能是: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]蓝莲花

你可能感兴趣的:(day15)