美文网首页
Day16-作业

Day16-作业

作者: 萌王史莱姆 | 来源:发表于2019-01-14 21:48 被阅读0次

    0.定义一个学生类。有属性:姓名、年龄、成绩(语文,数学,英语)[每课成绩的类型为整数]
    方法:
    a. 获取学生的姓名:getname()
    b. 获取学生的年龄:getage()
    c. 返回3门科目中最高的分数。get_course()

    class Student:
    
        def __init__(self):
            self._name = ''
            self._age = 0
            self._score = {}
    
        @property
        def getname(self):
            return self._name
    
        @getname.setter
        def name(self, name):
            if not isinstance(name, str):
                raise ValueError
    
            if not (2 <= len(name) <= 4):
                raise ValueError
    
            self._name = name
    
        @property
        def getage(self):
            return self._age
    
        @getage.setter
        def age(self, age):
            if not isinstance(age, int):
                raise ValueError
    
            if not (0 <= age <= 100):
                raise ValueError
    
            self._age = age
    
        @property
        def getscore(self):
            return self._score
    
        @getscore.setter
        def score(self, score: dict):
            for key in score:
                if not isinstance(score[key], int):
                    raise ValueError
    
                if not (0 <= score[key] <= 100):
                    raise ValueError
    
            self._score = score
    
        def get_course(self):
            value_list = self._score.values()
            return max(value_list)
    
    
    def main():
        stu1 = Student()
        stu1.name = '张三'
        stu1.age = 18
        stu1.score = {'语文': 60, '数学': 90, '英语': 85}
    
        print(stu1.getname)
    
        print(stu1.getage)
    
        print(stu1.get_course())
    
    
    if __name__ == '__main__':
        main()
    

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

    class Auto:
    
        def __init__(self, color, weight):
            self.tyre_number = 4
            self.color = color
            self.weight = weight
            self._speed = 0
    
        @staticmethod
        def speed_up():
            print('汽车加速')
    
        @staticmethod
        def speed_down():
            print('汽车减速')
    
        @staticmethod
        def stop():
            print('汽车停车')
    
        @property
        def speed(self):
            return self._speed
    
        @speed.setter
        def speed(self, speed):
            if not (0 <= speed <= 300):
                raise ValueError
    
            self._speed = speed
    
        def __gt__(self, other):
            return self.speed > other
    
    
    class CarAuto(Auto):
        def __init__(self, color, weight):
            super().__init__(color, weight)
            self.air_conditioner = '有'
            self.CD = '有'
    
        @staticmethod
        def speed_up():
            print('小汽车加速了')
    
        @staticmethod
        def speed_down():
            print('小汽车减速了')
    
    
    def main():
        auto = Auto('黑色', '5T')
    
        auto.speed = 100
        print(auto.tyre_number, auto.color, auto.weight, auto.speed)
    
        change_speed = int(input('请输入新的速度:'))
        if change_speed == 0:
            Auto.stop()
            auto.speed = change_speed
            print(auto.speed)
        elif auto.speed > change_speed:
            Auto.speed_down()
            auto.speed = change_speed
            print(auto.speed)
        else:
            Auto.speed_up()
            auto.speed = change_speed
            print(auto.speed)
    
        carauto = CarAuto('白色', '3T')
    
        carauto.speed = 100
        print(carauto.tyre_number, carauto.color, carauto.weight, carauto.speed, carauto.air_conditioner, carauto.CD)
    
        change_speed = int(input('请输入新的速度:'))
        if carauto.speed > change_speed:
            CarAuto.speed_down()
            carauto.speed = change_speed
            print(carauto.speed)
        else:
            CarAuto.speed_up()
            carauto.speed = change_speed
            print(carauto.speed)
    
    
    if __name__ == '__main__':
        main()
    

    2.创建一个名为User 的类,其中包含属性firstname 和lastname ,还有用户简介通常会存储的其他几个属性。
    在类User 中定义一个名 为describeuser() 的方法,它打印用户信息摘要;
    再定义一个名为greetuser() 的方法,它向用户发出个性化的问候。

    管理员是一种特殊的用户。编写一个名为Admin 的类,让它继承User类。
    添加一个名为privileges 的属性,用于存储一个由字符串(如"can add post"、"can delete post"、"can ban user"等)组成的列表。
    编写一个名为show_privileges()的方法,它显示管理员的权限。创建一个Admin 实例,并调用这个方法。

    class User:
        def __init__(self, firstname='', lastname='', age=0, sex='', tel=0):
            self._firstname = firstname
            self._lastname = lastname
            self._age = age
            self._sex = sex
            self._tel = tel
    
        @property
        def firstname(self):
            return self._firstname
    
        @firstname.setter
        def firstname(self, firstname):
            if not (1 <= len(firstname) <= 2):
                raise ValueError
    
            self._firstname = firstname
    
        @property
        def lastname(self):
            return self._lastname
    
        @lastname.setter
        def lastname(self, lastname):
            if not (1 <= len(lastname) <= 2):
                raise ValueError
    
            self._lastname = lastname
    
        @property
        def age(self):
            return self._age
    
        @age.setter
        def age(self, age):
            if not (0 <= age <= 100):
                raise ValueError
    
            self._age = age
    
        @property
        def sex(self):
            return self._sex
    
        @sex.setter
        def sex(self, sex):
            self._sex = sex
    
        @property
        def tel(self):
            return self._tel
    
        @tel.setter
        def tel(self, tel):
            if not len(str(tel)) == 11:
                raise ValueError
    
            self._tel = tel
    
        def describeuser(self):
            print('姓名:' + self.firstname + self.lastname + ' ' + '年龄:' + str(self.age) + ' ' + '性别:' + self.sex + ' ' +
                  '电话:' + str(self.tel))
    
        def greetuser(self):
            print('hello! %s' % self.firstname + self.lastname)
    
    
    class Admin(User):
        def __init__(self, firstname='', lastname='', age=0, sex='', tel=0, privileges=[]):
            super().__init__(firstname, lastname, age, sex, tel)
            self.privileges = privileges
    
        def show_privileges(self):
            for item in self.privileges:
                print('%s %s' % (self.firstname + self.lastname, item))
    
    
    def main():
        user = User()
    
        firstname = input('请输入姓:')
        user.firstname = firstname
    
        lastname = input('请输入名:')
        user.lastname = lastname
    
        age = int(input('请输入年龄:'))
        user.age = age
    
        sex = input('请输入性别:')
        user.sex = sex
    
        tel = int(input('请输入电话:'))
        user.tel = tel
    
        user.describeuser()
        user.greetuser()
    
        admin = Admin()
    
        firstname = input('请输入姓:')
        admin.firstname = firstname
    
        lastname = input('请输入名:')
        admin.lastname = lastname
    
        age = int(input('请输入年龄:'))
        admin.age = age
    
        sex = input('请输入性别:')
        admin.sex = sex
    
        tel = int(input('请输入电话:'))
        admin.tel = tel
    
        admin.privileges = ['can add post', 'can delete post', 'can ban user']
        admin.show_privileges()
    
    
    if __name__ == '__main__':
        main()
    

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

    class Person:
        num = 0
    
        def __init__(self):
            self.name = '张三'
            Person.count()
    
        @classmethod
        def count(cls):
            cls.num += 1
            return cls.num
    
    
    def main():
        p1 = Person()
        print(Person.num)
    
        p2 = Person()
        print(Person.num)
    
        p3 = Person()
        print(Person.num)
    
    
    
    if __name__ == '__main__':
        main()
    

    相关文章

      网友评论

          本文标题:Day16-作业

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