美文网首页
May 6th_day15_homework

May 6th_day15_homework

作者: 3ae1c50960af | 来源:发表于2019-05-09 09:55 被阅读0次

    question1.建立一个汽车类Auto,包括轮胎个数,汽车颜色,车身重量,速度等属性,并通过不同的构造方法创建实例。至少要求 汽车能够加速 减速 停车。

    再定义一个小汽车类CarAuto 继承Auto 并添加空调、CD属性,并且重新实现方法覆盖加速、减速的方法

    class Auto:
        def __init__(self, make, model, weight, color='白色', tyre=4):
            self.make = make.capitalize()
            self.model = model.capitalize()
            self.weight = weight
            self.tyre = tyre
            self.color = color
            self.speed = 0
    
        def show_car(self):
            print('这是一辆%s %s,颜色:%s,车重:%dkg,轮胎:%d个' % (self.make, self.model, self.color, self.weight, self.tyre))
    
        def update_speed(self, value):
            if self.speed == 0:
                self.speed = value
                print('初始化车速成功')
                self.show_speed()
            else:
                print('错误:只能对速度为0的车初始化速度')
    
        def increment_speed(self, value):
            self.speed += value
            print('加速成功')
            self.show_speed()
    
        def decrement_speed(self, value):
            if value > self.speed:
                print('错误:减少速度大于汽车当前速度')
            elif value == self.speed:
                self.stop()
            else:
                self.speed -= value
                print('减速成功')
                self.show_speed()
    
        def stop(self):
            self.speed = 0
            print('停车成功')
            print('%s %s已停车' % (self.make, self.model))
    
        def show_speed(self):
            print('%s %s当前车速为%dkm/h' % (self.make, self.model, self.speed))
    
    
    car1 = Auto('audi', 'a4', 1560)
    car2 = Auto('BMW', '320i', 1570, '黑色')
    car3 = Auto('benz', 'glc260', 1860, '蓝色', 4)
    
    car1.show_car()
    car2.show_car()
    car3.show_car()
    
    car1.show_speed()
    car1.update_speed(20)
    car1.increment_speed(40)
    car1.decrement_speed(30)
    car1.stop()
    """
    这是一辆Audi A4,颜色:白色,车重:1560kg,轮胎:4个
    这是一辆Bmw 320i,颜色:黑色,车重:1570kg,轮胎:4个
    这是一辆Benz Glc260,颜色:蓝色,车重:1860kg,轮胎:4个
    Audi A4当前车速为0km/h
    初始化车速成功
    Audi A4当前车速为20km/h
    加速成功
    Audi A4当前车速为60km/h
    减速成功
    Audi A4当前车速为30km/h
    停车成功
    Audi A4已停车
    """
    
    
    class CarAuto(Auto):
        def __init__(self, make, model, weight, color='白色', tyre=4, air_conditioner=0, cd=0):
            super().__init__(make, model, weight, color, tyre)
            self._air_conditioner = air_conditioner
            self._cd = cd
    
        @property
        def air_conditioner(self):
            if self._air_conditioner:
                print('这辆车有空调,可以吹空调')
                return self._air_conditioner
            else:
                print('这辆车没空调,好蠢')
                return self.air_conditioner
    
        @property
        def cd(self):
            if self._cd:
                print('这辆车有CD,可以听音乐')
                return self._cd
            else:
                print('这辆车没CD,好蠢')
                return self._cd
    
        def show_car(self):
            super().show_car()
            self.air_conditioner
            self.cd
    
        def useless(self):
            if self._air_conditioner and self._cd:
                print('吹着空调听着音乐好爽')
            elif self._air_conditioner:
                print('只吹空调一般爽')
            elif self._cd:
                print('只听音乐一般般')
            else:
                print('又没空调又没音乐贼难受')
    
        def increment_speed(self, value):
            super().increment_speed(value)
            self.useless()
    
        def decrement_speed(self, value):
            super().decrement_speed(value)
            self.useless()
    
    
    car4 = CarAuto('tesla', 'model s', 1660, '银色', 4, 1)
    car4.show_car()
    car4.update_speed(30)
    car4.increment_speed(50)
    car4.decrement_speed(30)
    car4.stop()
    """
    这是一辆Tesla Model s,颜色:银色,车重:1660kg,轮胎:4个
    这辆车有空调,可以吹空调
    这辆车没CD,好蠢
    初始化车速成功
    Tesla Model s当前车速为30km/h
    加速成功
    Tesla Model s当前车速为80km/h
    只吹空调一般爽
    减速成功
    Tesla Model s当前车速为50km/h
    只吹空调一般爽
    停车成功
    Tesla Model s已停车
    """
    

    question2.创建一个Person类,添加一个类字段用来统计Person类的对象的个数

    class Person:
        x = (x for x in range(1, 10000))
        count = 0
    
        def __init__(self, name, age):
            self.name = name
            self.age = age
            Person.count = next(Person.x)
    
    
    p1 = Person('张三', 20)
    print('%s类当前共创建%d个对象' % (Person.__name__, Person.count))
    p2 = Person('李四', 30)
    print('%s类当前共创建%d个对象' % (Person.__name__, Person.count))
    p3 = Person('王五', 40)
    print('%s类当前共创建%d个对象' % (Person.__name__, Person.count))
    p4 = Person('赵一', 40)
    print('%s类当前共创建%d个对象' % (Person.__name__, Person.count))
    """
    Person类当前共创建1个对象
    Person类当前共创建2个对象
    Person类当前共创建3个对象
    Person类当前共创建4个对象
    """
    

    question3.创建一个动物类,拥有属性:性别、年龄、颜色、类型 ,

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

    class Animals:
        def __init__(self, gender, age, color, species):
            self.gender = gender
            self.age = age
            self.color = color
            self.species = species
    
        def __repr__(self):
            return '%s的对象:性别-%s 年龄-%d岁 颜色-%s 类型-%s' % (Animals.__name__,
                                                       self.gender, self.age, self.color, self.species)
    
    
    pet1 = Animals('公', 5, '白色', '泰迪犬')
    print(pet1)
    """
    Animals的对象:性别-公 年龄-5岁 颜色-白色 类型-泰迪犬
    """
    

    question4.写一个圆类, 拥有属性半径、面积和周长;要求获取面积和周长的时候的时候可以根据半径的值把对应的值取到。

    但是给面积和周长赋值的时候,程序直接崩溃,并且提示改属性不能赋值

    class WriteError(Exception):
        def __str__(self):
            return '不能修改只读的属性'
    
    
    class Circle:
        pi = 3.1415926
    
        def __init__(self, radius=0):
            self._radius = radius
            self._perimeter = 0
            self._area = 0
    
        @property
        def radius(self):
            return self._radius
    
        @radius.setter
        def radius(self, value):
            self._radius = value
            self._perimeter = 2 * Circle.pi * self._radius
            self._area = Circle.pi * self._radius ** 2
    
        @property
        def perimeter(self):
            return self._perimeter
    
        @perimeter.setter
        def perimeter(self, value):
            raise WriteError
    
        @property
        def area(self):
            return self._area
    
        @area.setter
        def area(self, value):
            raise WriteError
    
    
    r1 = Circle()
    r1.radius = 5
    print(r1.perimeter, r1.area)
    # r1.perimeter = 20
    """
    31.415926 78.539815
    Traceback (most recent call last):
        raise WriteError
    __main__.WriteError: 不能修改只读属性
    """
    

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

    歌词前面对应的是时间,在对应的时间点可以显示对应的歌词

    import time
    
    
    """
    class FileOperation:
        def __init__(self, filename):
            self.filename = filename
    
        def read_txt(self):
            temp_list = []
            with open(self.filename) as file_obj:
                for line in file_obj:
                    temp_list.append(line.rstrip())
            return temp_list
    """
    
    
    class LyricManage:
        def __init__(self, filename):
            self.filename = filename
            self._lyric = None
            self._dict_lyric = None
    
        @property
        def lyric(self):
            temp_list = []
            with open(self.filename) as file_obj:
                for line in file_obj:
                    temp_list.append(line.rstrip())
            self._lyric = temp_list
            return self._lyric
    
        @property
        def dict_lyric(self):
            temp_dict = {}
            for lis in self.lyric:
                lis_1 = lis.split(']')
                for i in range(len(lis_1)-1):
                    key = float(lis_1[i][1:3]) * 60 + float(lis_1[i][4:9])
                    value = lis_1[-1]
                    temp_dict[key] = value
            self._dict_lyric = dict(sorted(temp_dict.items()))
            return self._dict_lyric
    
        @dict_lyric.setter
        def dict_lyric(self, value):
            raise ValueError
    
        def play_lyric(self):
            start_time = time.clock()
            for key in self.dict_lyric:
                while True:
                    end_time = time.clock()
                    if round(key, 2) == round(end_time, 2):
                        print(self.dict_lyric[key])
                        break
    
    
    lyric1 = LyricManage('lyric/lanlianhua.txt')
    lyric1.play_lyric()
    

    相关文章

      网友评论

          本文标题:May 6th_day15_homework

          本文链接:https://www.haomeiwen.com/subject/grvsoqtx.html