day13-homework

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

class Comps:
    def __init__(self, brand, color='black'):
        self.brand = brand
        self.color = color
        self.ram = 4

    @staticmethod
    def func1():
        print('打游戏','写代码','看视频')


# a.
com1 = Comps('apple')
print(com1.brand)
com1.color = 'white'
print(com1.color)
com1.ram = '8GB'
print(com1.ram)
del com1.brand
# print(com1.brand)

# b.
com2 = Comps('Huawei', color='white')
print(getattr(com2, 'brand'))  # 获取
setattr(com2, 'color', 'gold')  # 修改
print(com2.color)
setattr(com2, 'price', 8000)  # 添加
print(com2.price)
delattr(com2, 'ram')  # 删除

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

class Dogs:
    def __init__(self, name1, color='yellow', age1=2):
        self.name1 = name1
        self.color = color
        self.age1 = age1

    def bark(self):
        print('%s在嗷嗷叫!' % self.name1)


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

    def walk_dog(self):
        if self.dog:
            print('%s牵着%s在散步' %(self.name, self.dog.name1))
        else:
            print('没有狗!溜自己~')


p1 = Person('小明', 18)
dog1 = Dogs('大黄')
p1.dog = dog1
p1.walk_dog()

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

import math

class Circle:
    def __init__(self, radius, x=0, y=0):
        self.radius = radius
        self.center_x = x
        self.center_y = y

    def area(self):
        return math.pi * self.radius * self.radius

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


c1 = Circle(3)
print(c1.area(), c1.perimeter())

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

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

class Student:
    def __init__(self, name, age=0, num='0001'):
        self.name = name
        self.age = age
        self.num = num
        self.state = randint(0, 1)   # 是否在教室的状态  0 -->不在  1 --> 在

    def amount(self):
        print('%s 到!' % self.name)

    def show(self):
        print('姓名:%s 年龄:%d 学号:%s' % (self.name, self.age, self.num))


class Class:
    def __init__(self, name):
        self.students = []
        self.name = name
        # 学号生成器
        self.nums =('stu'+str(num).zfill(3) for num in range(50))

    # 添加学生
    def add_student(self):
        # 输入学生信息
        stu_name = input('姓名:')
        stu_age = int(input('年龄:'))
        stu_num = next(self.nums)
        # 创建学生对象
        stu = Student(stu_name, stu_age, stu_num)
        # 将学生添加到班级中
        self.students.append(stu)

    # 删除学生
    def del_student(self):
        del_name = input('删除学生的姓名')
        for stu in self.students[:]:
            if stu.name == del_name:
                self.students.remove(stu)

    def call_the_roll(self):
        for stu in self.students:
            print(stu.name)
            if stu.state:
                # 答到
                stu.amount()
            else:
                print('....')

    def average_age(self):
        sum1 = 0
        for stu in self.students:
            sum1 += stu.age
        average = sum1//len(self.students)
        print('平均年龄: %d' % average)
        return average

    def cl_show(self):
        for stu in self.students:
            stu.show()

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