day15作业

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

    def speed_up(self, speed):
        print('速度增加到了%dkm/h' % speed)

    def speed_dowm(self, speed):
        print('速度增加到了%dkm/h' % speed)

    def speed_stop(self, speed):
        print('速度为%dkm/h,停车了' % speed)


class CarAuto(Auto):
    def __init__(self, tire=4, color='黑色', weight=500, speed=200, air=False, cd=True):
        super().__init__()
        self.tire = tire
        self.color = color
        self.weight = weight
        self.speed = speed
        self.air = air
        self.cd = cd

    @classmethod
    def speed_up(cls, speed):
        print('速度增加到了%dkm/h' % speed)

    @classmethod
    def speed_dowm(cls, speed):
        print('速度增加到了%dkm/h' % speed)

    @classmethod
    def speed_stop(cls, speed):
        print('速度为%dkm/h,停车了' % speed)


c1 = CarAuto(speed=100, air=True)
c2 = CarAuto(weight=1000)
c3 = CarAuto(color='绿色')
c4 = CarAuto(tire=5)
print(c1.__dict__)
print(c2.__dict__)
print(c3.__dict__)
print(c4.__dict__)
CarAuto.speed_up(200)
CarAuto.speed_dowm(50)
CarAuto.speed_stop(0)
  1. 创建一个Person类,添加一个类字段用来统计Perosn类的对象的个数
class Person:
    count = 0

    def __init__(self):
        Person.count += 1


p1 = Person()
p2 = Person()
p3 = Person()
print('Person类的对象有%d个' % Person.count)
  1. 创建一个动物类,拥有属性:性别、年龄、颜色、类型 ,

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

class Animal:
    def __init__(self, gender, age, color, types):
        self.gender = gender
        self.age = age
        self.color = color
        self.types = types

    def print_out(self):
        return '性别-%s 年龄-%s 颜色-%s 类型-%s' % (self.gender, self.age, self.color, self.types)


animal = Animal('公', '3岁', '黑色', '老虎')
print(Animal.__name__, '的对象:', animal.print_out())
  1. 写一个圆类, 拥有属性半径、面积和周长;要求获取面积和周长的时候的时候可以根据半径的值把对应的值取到。但是给面积和周长赋值的时候,程序直接崩溃,并且提示改属性不能赋值
import math


class Circle:
    def __init__(self, r):
        self._r = r
        self._perimeter = 2 * math.pi * r
        self._area = math.pi * r**2

    @property
    def r(self):
        return self._r

    @r.setter
    def r(self, value):
        self._r = value
        self._perimeter = 2 * math.pi * self._r
        self._area = math.pi * self._r**2

    @property
    def perimeter(self):
        return self._perimeter

    @perimeter.setter
    def perimeter(self, value):
        raise ValueError

    @property
    def area(self):
        return self._area

    @area.setter
    def area(self, value):
        raise ValueError


r1 = Circle(5)
print('周长:', r1.perimeter, '面积:', r1.area)

r1.r = 2
print('周长:', r1.perimeter, '面积:', r1.area)

  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作业)