day13homework

import math
1.声明电脑类: 属性:品牌、颜色、内存大小 方法:打游戏、写代码、看视频

a.创建电脑类的对象,然后通过对象点的方式获取、修改、添加和删除它的属性
b.通过attr相关方法去获取、修改、添加和删除它的属性

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

    @classmethod
    def play_game(cls):
        print('正在打游戏')

    def write_code(self):
        print('正在写代码')

    @staticmethod
    def watch_video():
        print('正在看视频')


com1 = Computer('Lenovo', 'black', 500)
print(com1.storage, com1.color, com1.brand)
com1.storage = 600
com1.size = 24
print(com1.size)
del com1.size

print(getattr(com1, 'brand', '未知品牌'))
setattr(com1, 'storage', 800)
setattr(com1, 'size', 30)
print(com1.storage, com1.color, com1.brand, com1.size)
delattr(com1, 'size')

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

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

    def wow(self):
        print('旺!')


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

    def walk_dog(self):
        print(self.name+'正在遛'+self.dog.name)
        self.dog.wow()


doge1 = Dog('大黄', 'yellow', 2)
person1 = Person('小明', 25, doge1)
person1.walk_dog()

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

class Circle:
    """定义一个圆,有位置,半径的属性 可以计算周长,面积,可以移动位置"""
    def __init__(self, pos_x: int, pos_y: int, radius: int):
        self.x = pos_x
        self.y = pos_y
        self.radius = radius

    def move(self, x_, y_):
        self.x += x_
        self.y += y_

    def area_perimeter(self):
        return math.pi * self.radius ** 2, 2 * math.pi * self.radius

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

class Students:
    def __init__(self, name, age):
        self.name = name
        self.age = age
        self.num = '001'

    def response(self):
        print(self.name+'到')

    def show_information(self):
        print(self.name, self.age, self.num)


class Class:
    def __init__(self, name, student=list()):
        self.name = name
        self.students = student

    def add_stu(self, stu: object):
        self.students.append(stu)

    def remove_stu(self, stu: object):
        self.students.remove(stu)

    def call_roll(self):
        for stu in self.students:
            print(stu.name)
            stu.response()

    def stu_avg_age(self):
        sum_age = 0
        num = 0
        for stu in self.students:
            sum_age += stu.age
            num += 1
        return round(sum_age/num, 1)


stu1 = Students('小华', 23)
stu2 = Students('小明', 22)
stu3 = Students('小黑', 28)

cls1 = Class('py1901')
cls1.add_stu(stu1)
cls1.add_stu(stu2)
cls1.add_stu(stu3)
cls1.call_roll()
print(cls1.stu_avg_age())

你可能感兴趣的:(day13homework)