美文网首页
Day15-作业

Day15-作业

作者: 略略略_29fd | 来源:发表于2019-08-12 09:20 被阅读0次

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

    class Auto:
        """汽车类:Auto,
        属性:汽车颜色,重量,速度,轮胎个数"""
        def __init__(self, color, weight, speed, tires_num):
            self.color = color
            self.weight = weight
            self.speed = speed
            self.tires_num = tires_num
    
        def speed_up(self, value):
            self.speed += 1
    
        def speed_down(self, value):
            self.speed -= 1
    
        def parking(self, value):
            self.speed = 0
    
    
    class CarAuto(Auto):
        """小汽车类,继承Auto
        属性添加:空调,cd属性"""
        def __init__(self, color, weight, speed, tires_num, air_con, cd):
            super().__init__(color, weight, speed, tires_num)
            self.air_con = air_con
            self.cd = cd
    
        def speed_up(self, value):
            self.speed += 2
            print('当前速度加速到%s' % self.speed)
    
        def speed_down(self, value):
            self.speed -= 2
            print('当前速度减速到%s' % self.speed)
    

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

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

    创建一个动物类,拥有属性:性别、年龄、颜色、类型 ,
    要求打印这个类的对象的时候以'/XXX的对象:
    性别-? 年龄-? 颜色-? 类型-?/' 的形式来打印

    
    
    
    class Animal:
        """类:动物
        属性:性别、年龄、颜色、类型"""
        def __init__(self, sex='公', age=0, color='yellow', type1='cat'):
            self.sex = sex
            self.age = age
            self.color = color
            self.type1 = type1
    
        def __str__(self):
            return str(self.__class__)+'的对象:性别-'+self.sex+', 年龄-'+str(self.age)+', 颜色-'+str(self.color)+', 类型-'+str(self.type1)
    
    animal = Animal('母', 2, 'black', 'dog')
    print(animal)
    

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

    class WriteError(Exception):
        def __str__(self):
            return '该属性不能赋值!'
    
    
    class Circle:
        """类:圆
        属性:半径,面积,周长"""
        pi = 3.1415926
    
        def __init__(self, radius):
            self.r = radius
            self._area = 0
            self._per = 0
    
        @property
        def area(self):
            return Circle.pi * self.r ** 2
    
        @area.setter
        def area(self, value):
            raise WriteError
    
        @property
        def per(self):
            return 2 * Circle.pi * self.r
    
        @per.setter
        def per(self, value):
            raise WriteError
    
    
    c1 = Circle(2)
    print(c1.per)
    
    c1.radius = 3
    print(c1.per)
    
    import random
    

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

    class Poke:
        poke = []  # 扑克牌牌堆
        p1 = []   # 玩家一牌堆
        p2 = []   #  玩家二牌堆
        p3 = []    #  玩家三牌堆
        last = None   # 底牌牌堆
    
        def __init__(self,f,num):      # 初始化牌堆
            self.flower = f     # 花色
            self.num = num    #  点数
    
        def __str__(self):
            return "%s%s" % (self.flower,self.num)     # 返回牌值
    
        @classmethod
        def init(cls):   # 定义牌堆
            ph = ("♥","♠","♣","♦")                    # 花色元组
            pnum = ("2","3","4","5","6","7","8","9","10","J","Q","K","A")  # 点数元组
            king = {"big":"大王","small":"小王"}        # 大小王
            for p in ph:                   # 循环遍历花色
                for _nump in pnum:    #  循环遍历点数
                    cls.poke.append(Poke(p,_nump))  # 装牌
            cls.poke.append(Poke(king["big"],""))   # 装大王
            cls.poke.append(Poke(king["small"],""))  # 装小王
    
        @classmethod
        def wash(cls):     # 洗牌
            random.shuffle(cls.poke)
    
        @classmethod
        def send(cls):    #  发牌
            for _ in range(0,17): # 三个人每人发17张牌 循环
                cls.p1.append(cls.poke.pop(0))   # 玩家一发牌
                cls.p2.append(cls.poke.pop(0))
                cls.p3.append(cls.poke.pop(0))
            cls.last= tuple(cls.poke)            # 最后三张牌做底牌  不能修改做元组
    
        @classmethod
        def show(cls):    # 展示牌
            print("gamer1:")
            for pokes in cls.p1:
                print(pokes,end = " ")
            print()
            print("gamer2:")
            for pokes in cls.p2:
                print(pokes, end=" ")
            print()
            print("gamer3:")
            for pokes in cls.p3:
                print(pokes, end=" ")
            print()
            print("ending:")
            for pokes in cls.last:
                print(pokes, end=" ")
            print()
    
    
    Poke.init()
    Poke.wash()
    Poke.send()     #  必须先发牌后才能展示牌
    Poke.show()
    

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

    
    class Lyrics:
        """类:歌词"""
        pass
    
    
    class Parsing:
        """
        类:解析器
        """
        pass
    

    相关文章

      网友评论

          本文标题:Day15-作业

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