Day 15 课后作业

声明一个电脑类: 属性:品牌、颜色、内存大小
方法: 打游戏、写代码、看视频
a.创建电脑类的对象,然后通过对象点的方式获取、修改、添加和删除它的属性
b.通过 attr 相关方法去获取、修改、添加和删除它的属性

class Computer:
    def __init__(self, brand : str, color : str, memory_capacity : str):
        self.brand = brand
        self.color = color
        self.memory_capacity = memory_capacity

    @classmethod
    def play_games(cls):
        pass

    @classmethod
    def write_code(cls):
        pass

    @classmethod
    def watch_video(cls):
        pass

a.通过对象点的方式获取、修改、添加和删除它的属性

 print(c1.brand, c1.color, c1.memory_capacity)
    c1.memory_capacity = '16G'
    c1.system = 'windows'
    del c1.color

b.通过attr相关方法去获取、修改、添加和删除它的属性

print(getattr(c1, 'memory_capacity',))
    setattr(c1,'brand','联想')
    print(c1.brand)
    setattr(c1, 'color', '黑色')
    print(c1.color)
    delattr(c1,'brand')
    print(c1.__dict__)

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

class Person:
    def __init__(self,name : str, age :int , dog : str):
        self.name = name
        self.age = age
        self.dog = dog


    def walk_the_dog(self,dog_name):
        print('%s在遛%s' % (self.name , dog_name))


class Dog:
    def __init__(self , name : str , color : str , age : int):
        self.name = name
        self.color = color
        self.age = age


    def bark(self):
        print('%s在叫唤' % self.name)
 p1 = Person('小明',18,'大黄')
    d1 = Dog('大黄','黄色',3)
    p1.walk_the_dog(d1.name)

声明一个圆类:

class Circle:
    def __init__(self,  radius : float):
        self.radius = radius

创建一个学生类:
属性: 姓名,年龄,学号
方法: 答到,展示学生信息
创建一个班级类:
属性: 学生,班级名
方法: 添加学生,删除学生,点名, 求班上学生的平均年龄

class Student:
    def __init__(self, name: str, age: int, stu_num: str):
        self.name = name
        self.age = age
        self.stu_num = stu_num

    def answer(self):
        print('%s到!' % self.name, self.__dict__)


class Class:
    def __init__(self, class_name: str, *student):
        student_info = []
        for item in student:
            student_info.append(item.__dict__)
        self.student = student_info
        self.class_name = class_name

    def add_stu(self, student: Student):
        self.student.append(student.__dict__)

    def del_stu(self, student: Student):
         studentinfo = student.__dict__
         for stu in self.student:
             if stu['stu_num'] == studentinfo['stu_num']:
                 self.student.remove(stu)
                 print('删除成功!')

    def call_names(self, student: Student):
        for object in self.student:
            if object['name'] == student.name:
                student.answer()

    def average_age(self):
        sum = 0
        for student in self.student:
            sum += student['age']
        return sum / len(self.student)
s1 = Student('小明', 15, 'py001')
    s2 = Student('小华', 17, 'py003')
    s3 = Student('小红', 18, 'py002')
    s4 = Student('李源', 18, 'py004')
    c1 = Class('Python1班',s1,s2,s3)
    print(c1.student)
    c1.add_stu(s4)
    print(c1.student)
    c1.del_stu(s4)
    print(c1.student)
    c1.call_names(s3)
    print('班上学生的平均年龄是%.1f岁' % c1.average_age())

你可能感兴趣的:(Day 15 课后作业)