day15作业

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

    def __init__(self, color, weight, speed, tire=4):
        self.tire = tire
        self.color = color
        self.weight = weight
        self._speed = speed

    @property
    def speed(self):
        return self._speed

    @speed.setter
    def speed(self, value):
        if not isinstance(value, int):
            raise ValueError
        elif value < self._speed:
            self._speed = value
            print("减速")
        elif value > self._speed:
            self._speed = value
            print("加速")
        elif self._speed == 0:
            self._speed = value
            print("停车")
        elif self._speed == value:
            print("保持当前车速")


class CarAuto(Auto):
    def __init__(self, cd, conditioning, color, weight, speed):
        super().__init__(color, weight, speed)
        self.cd = cd
        self.conditioning = conditioning

    @property
    def speed(self):
        return self._speed

    @speed.setter
    def speed(self, value):
        if not isinstance(value, int):
            raise ValueError
        elif value < self._speed:
            self._speed = value
            print("减速")
        elif value > self._speed:
            self._speed = value
            print("加速")
        elif self._speed == 0:
            self._speed = value
            print("停车")
        elif self._speed == value:
            print("保持当前车速")

    def cd(self):
        print("正在播放%s" % self.cd)

    def conditioning(self):
        print("当前空调温度%s" % self.conditioning)
  1. 创建一个Person类,添加一个类字段用来统计Perosn类的对象的个数
class Person:
    num = 0

    def __init__(self, name):
        Person.num += 1
        self.name = name


p1 = Person("田宇")
p2 = Person("刘茂飞")
print(Person.num)
  1. 创建一个动物类,拥有属性:性别、年龄、颜色、类型 ,
    要求打印这个类的对象的时候以'/XXX的对象: 性别-? 年龄-? 颜色-? 类型-?/' 的形式来打印
class Animal:
    def __init__(self, age, color, breed, name):
        self._gender = 1
        self.age = age
        self.color = color
        self.breed = breed
        self.name = name

    @property
    def gender(self):
        if self._gender == 1:
            return '雌'
        return '雄'

    @gender.setter
    def gender(self, value):
        if value == "雌":
            self._gender = 1
        elif value == "雄":
            self._gender = 0

    def attributes(self):
        return "%s的对象:性别-%s,年龄-%s,颜色-%s,类型-%s" % (self.name, self.gender, self.age, self.color, self.breed)


p1 = Animal(3, "black", "金毛", "辛巴达")
p1.gender = "雄"
print(p1.attributes())
4. 写一个圆类, 拥有属性半径、面积和周长;要求获取面积和周长的时候的时候可以根据半径的值把对应的值取到。但是给面积和周长赋值的时候,程序直接崩溃,并且提示改属性不能赋值
```python
from math import *


class WriteError(Exception):
    def __str__(self):
        return "属性不能赋值"


class Circle:

    def __init__(self, r):
        self.r = r
        self._area = pi * r ** 2
        self._perimeter = 2 * pi * r

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

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


p1 = Circle(5)
p1.area = 111
  1. 写一个扑克类, 要求拥有发牌和洗牌的功能(具体的属性和其他功能自己根据实际情况发挥)
  2. (尝试)写一个类,其功能是: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作业)