美文网首页
2018-10-17 作业

2018-10-17 作业

作者: hfudhu | 来源:发表于2018-10-18 20:10 被阅读0次
    1. 声明一个电脑类
      属性:品牌、颜色、内存大小
      方法:打游戏、写代码、看视频
      a.创建电脑类的对象,然后通过对象点的方式获取、修改、添加和删除它的属性
      b.通过attr相关方法去获取、修改、添加和删除它的属性
    
    class Computer:
        def __init__(self, brand, color, ram):
            self.brand = brand
            self.color = color
            self.ram = ram
    
        def play_game(self):
            print('打游戏')
    
        def coding(self):
            print('写代码')
    
        def watch_video(self):
            print('看视频')
    
    
    cp1 = Computer('联想', '黑色', 4)
    
    print(cp1.brand)
    print(getattr(cp1, 'brand', '苹果'))
    
    cp1.price = 5000
    setattr(cp1, 'price', 8000)
    
    cp1.color = '黑色'
    setattr(cp1, 'color', 5000)
    
    del cp1.ram
    delattr(cp1, 'color')
    

    2.声明一个人的类和狗的类:
    狗的属性:名字、颜色、年龄
    狗的 法:叫唤
    人的属性:名字、 年龄、狗
    人的方法:遛狗
    a.创建人的对象名字叫小明,让他拥有一条狗 ,然后让小明去遛狗

    class Dog:
        def __init__(self, name, color, age):
            self.name = name
            self.color = color
            self.age = age
    
        def shout(self):
            print('叫唤')
    
    
    class Person:
        def __init__(self, name: str, age: int):
            self.name = name
            self.age = age
            self.dog = None
    
        def took_the_dog(self):
            if self.dog:
                print('%s正牵着%s在散步' % (self.name, self.dog.name))
    
        def beat(self):
            if not self.dog:
                print('没有狗!')
    p1 = Person('小明', 18)
    dog = Dog('大黄', '黄色', 3)
    p1.dog = dog
    p1.took_the_dog()
    

    3.声明一个矩形类:
    属性: 长、宽# 方法:计算周长和面积
    a.创建不同的矩形,并且打印其周长和面积

    class Rect:
        def __init__(self, length, width):
            self.length = length
            self.width = width
    
        def perimeter(self):
            return (self.length + self.width)*2
    
        def area(self):
            return self.length * self.width
    
    
    rect1 = Rect(3, 2)
    print(rect1.perimeter(), rect1.area())
    
    rect2 = Rect(4, 5)
    print(rect2.perimeter(), rect2.area())
    

    4.创建一个学生类:
    属性:姓名,年龄,学号,成绩
    方法:答到,展示学生信息
    创建一个班级类: 属性:学生, 班级名
    方法:添加学生,删除学生,点名, 获取班级中所有学生的平均值, 获取班级中成绩最好的学生

    class Student:
        def __init__(self, name='', age=0, score=0, study_id=''):
            self.name = name
            self.age = age
            self.study_id = study_id
            self.score = score
    
        def replied(self):
            print('%s,到!' % self.name)
    
        def show_message(self):
            print(self.__dict__)
    
    
    import random
    
    
    class Class:
        creat_id = ('python'+str(x).rjust(3, '0') for x in range(1, 101))
    
        def __init__(self, name):
            self.students = []
            self.name = name
        def add_student(self):
            name = input('姓名:')
            age = int(input('年龄:'))
            id = next(Class.creat_id)
            score = random.randint(0, 100)
            stu = Student(name, age, score, id)
            self.students.append(stu)
            print('添加成功:')
            stu.show_message()
        def del_student(self):
            del_name = input('姓名:')
            count = len(self.students)   
    
            for stu in self.students.copy():
                if stu.name == del_name:
                    self.students.remove(stu)
    
            if count == len(self.students):
                print('没有该学生!')
    
        def call(self):
            for stu in self.students:
                print(stu.name)
                # 学生答到
                stu.replied()
    
        def average_score(self):
            scores = 0
            for stu in self.students:
                scores += stu.score
    
            return scores/len(self.students)
    
        def most_excellent_student(self):
            return max(self.students, key=lambda stu: stu.score)
    
    
    class1 = Class('python1807')
    for _ in range(5):
        class1.add_student()
    

    相关文章

      网友评论

          本文标题:2018-10-17 作业

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