2019-08-09-作业

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

    class Auto:
        def __int__(self, num, color, weight, speed):
            self.num = num
            self.color = color
            self.weight = weight
            self.speed = speed
        def speed_up(self):
            print('加速')
    
        @classmethod
        def speed_cut(slc):
            print('减速')
    
        @staticmethod
        def stop():
            print('停车')
    class CarAuto(Auto):
        def __init__(self, num, color, weight, speed, air_conditioner, CD):
            super().__int__(num, color, weight, speed)
            self.air_conditioner = air_conditioner
            self.CD = CD
    
        def speed_up(self):
            print('加速1')
    
        def speed_cut(self):
            print('减速1')
    
    
    car1 = CarAuto(9, 'red', '1600kg', '300km/h', '无', '无')
    car1.speed_cut()
    

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

    class Person:
        count = 0
        def __init__(self):
            Person.count += 1
    
    p1 = Person()
    P2 = Person()
    print(Person.count)
    

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

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

    class Animals:
        def __init__(self, gender, age, color, species):
            self.gender = gender
            self.age = age
            self.color = color
            self.species = species
        def show(self):
            print('/%s的对象:性别-%s 年龄-%s 颜色-%s 类型-%s/' % (a1.__class__.__name__, self.gender, self.age, self.color, self.species))
    a1 = Animals('公', 2, '黄色', '狗')
    a1.show()
    

  4. 写一个圆类, 拥有属性半径、面积和周长;要求获取面积和周长的时候的时候可以根据半径的值把对应的值取到。但是给面积和周长赋值的时候,程序直接崩溃,并且提示改属性不能赋值

    import math
    
    
    class WriteError(Exception):
        def __str__(self):
            return '修改只读属性!'
    
    
    class Circle:
        def __init__(self, r):
            if isinstance(r, int) or isinstance(r, float):
                self._r = r
            else:
                raise ValueError
            self._area = 0
            self._perimeter = 0
        @property
        def r(self):
            return self._r
        @r.setter
        def r(self, value):
            if not isinstance(value, int) or isinstance(value, float):
                raise  ValueError
            elif value < 0:
                raise ValueError
            self._r = value
        @property
        def area(self):
            self._area = math.pi * self._r ** 2
            return self._area
        @area.setter
        def area(self):
            print('area不能赋值')
            raise WriteError
        @property
        def perimeter(self):
            self._perimeter = math.pi * self._r * 2
            return self._perimeter
        @perimeter.setter
        def perimeter(self):
            print('perimeter不能赋值')
            raise WriteError
    

  5. 写一个扑克类, 要求拥有发牌和洗牌的功能(具体的属性和其他功能自己根据实际情况发挥)

    import random
    class Poker:
        colors = ['\u2660', '\u2663', '\u2665', '\u2666']
        kinds = [str(x) for x in range(2, 11)]
        kinds.extend(['A', 'J', 'Q', 'K'])
        cards = ['大王', '小王']
        for x in colors:
            for y in kinds:
                cards.append(x + y)
        def porkers(self):
            return self.cards
        def riffle(self):
          random.shuffle(Poker.cards)
          return Poker.cards
        def deal(self, num, rest_num = 0):
            if rest_num == 0:
                n = 54 // num
                for i in range(num):
                    person_card = Poker.cards[n*i:n*(i+1)]
                    print(person_card)
            else:
                n = (54 - rest_num) // num
                for i in range(num):
                    person_card = Poker.cards[n * i:n * (i + 1)]
                    print(person_card)
                rest_cards = Poker.cards[(54 - rest_num) : 55]
                print(rest_cards)
    
    p1 = Poker()
    print(p1.riffle())
    p1.deal(4,2)
    

  6. (尝试)写一个类,其功能是: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]蓝莲花  
    

你可能感兴趣的:(2019-08-09-作业)