day14-homework

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

class Computer:
    __slots__ = ('brand', 'color', 'ram', 'graphics_card', 'cpu')

    def __init__(self, brand, color, ram):
        self.brand = brand
        self.color = color
        self.ram = ram

    def play_game(self):
        pass

    def coding(self):
        pass

    def watch_video(self):
        pass


computer1 = Computer('MSI', 'black', '8G')
# a.
brand = computer1.brand    # 获取computer1的'brand'属性
computer1.brand = 'Lenovo'    # 修改computer1的'brand'属性
computer1.graphics_card = 'GTX765M'    # 给computer1添加'graphics_card'属性
del computer1.brand    # 删除computer1的'brand'属性
# b.
color = getattr(computer1, 'color')    # 获取computer1的'color'属性
setattr(computer1, 'color', 'red')    # 修改computer1的'color'属性
setattr(computer1, 'cpu', 'i5-4200M')    # 给computer1添加'cpu'属性
delattr(computer1, 'color')    # 删除computer1的'color'属性

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

class Human:
    def __init__(self, name, age, dog=None):
        self.name = name
        self.age = age
        self.dog = dog

    def walking_the_dog(self):
        if self.dog:
            return '{}正在溜{}'.format(self.name, self.dog.name)
        else:
            return '你没有狗!'


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

    def bark(self):
        return '{}:汪汪汪!'.format(self.name)


dog1 = Dog('大黄', '土黄色', 3)
p1 = Human('小明', 18)
p1.dog = dog1
print(p1.walking_the_dog())

3.声明一个圆类,自己确定有哪些属性和方法

class Circle:
    def __init__(self, r):
        self.r = r
        self.pi = 3.1415926

    def area(self):
        return self.pi*self.r**2

    def girth(self):
        return 2*self.pi*self.r

4.创建一个学生类:
属性:姓名,年龄,学号
方法:答到,展示学生信息

class Student:
    def __init__(self, name, age, study_id):
        self.name = name
        self.age = age
        self.study_id = str(study_id).zfill(3)

    def answer(self):
        return '{}:到!'.format(self.name)

    def info(self):
        return 'name:{} age:{} study_id:{}'.format(self.name, self.age, self.study_id)

5.创建一个班级类:
属性:学生,班级名
方法:添加学生,删除学生,点名, 求班上学生的平均年龄

import json


class Class:
    def __init__(self, class_name, students):
        self.students = students
        self.class_name = class_name

    def add_student(self, student):
        self.students.append(student)
        try:
            with open('student.json', 'r', encoding='utf-8') as f:
                classmates = json.loads(f.read())
        except (FileNotFoundError, json.decoder.JSONDecodeError):
            with open('student.json', 'w', encoding='utf-8') as f:
                f.write(json.dumps({}))
            with open('student.json', 'r', encoding='utf-8') as f:
                classmates = json.loads(f.read())
        classmates[self.class_name] = [student.info() for student in self.students]
        with open('student.json', 'w', encoding='utf-8') as f:
            f.write(json.dumps(classmates))

    def del_student(self, student):
        if student in self.students:
            self.students.remove(student)
            try:
                with open('student.json', 'r', encoding='utf-8') as f:
                    classmates = json.loads(f.read())
            except (FileNotFoundError, json.decoder.JSONDecodeError):
                with open('student.json', 'w', encoding='utf-8') as f:
                    f.write(json.dumps({}))
                with open('student.json', 'r', encoding='utf-8') as f:
                    classmates = json.loads(f.read())
            classmates[self.class_name] = [student.info() for student in self.students]
            with open('student.json', 'w', encoding='utf-8') as f:
                f.write(json.dumps(classmates))
        else:
            return '该学生不存在!'

    def call_the_roll(self, student):
        if student in self.students:
            return student.answer()

    def average_age(self):
        return sum([student.age for student in self.students])/len(self.students)


student1 = Student('小明', 18, 1)
student2 = Student('小花', 22, 2)
student3 = Student('小红', 20, 3)

class1 = Class('python1904', [student1, student2])
class1.add_student(student3)
print(class1.students)
print(class1.call_the_roll(student1))
print(class1.average_age())

你可能感兴趣的:(day14-homework)