day14-作业

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


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

    def play_game(self):
        print('打游戏')

    def write_code(self):
        print('敲代码')

    def watch_tv(self):
        print('看电视')
        
        
# a.
c1 = Computer('lenovo', 'black', '500G')
print(c1.brand)  # lenovo
c1.color = 'white'
c1.type = 'Thinkpad'
print(c1.color, c1.type)  # white Thinkpad
del c1.type
# print(c1.type)  # AttributeError: 'Computer' object has no attribute 'type'
# b.
print(getattr(c1, 'brand'))   # lenovo
setattr(c1, 'color', 'white')
setattr(c1, 'type', 'Thinkpad')
print(c1.color, c1.type)  # white Thinkpad
delattr(c1, 'type')
# print(c1.type)  # AttributeError: 'Computer' object has no attribute 'type'

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

class Person:
    """
    人的一个类
    name:人的姓名
    age:人的年龄
    dog:人的狗
    """
    __slots__ = ('name', 'age', 'dog')

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

    def walk_the_dog(self):
        print('%s溜他的%s' % (self.name, self.dog.name))


class Dog:
    """
    狗的一个类
    name: 狗的名字
    age: 狗的年龄
    color:狗的颜色
    """
    __slots__ = ('name', 'age', 'color')

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

    def call_out(self):
        print('%s,汪汪汪' % self.name)


dog1 = Dog('大黄', '黄色', 2)
p1 = Person('小明', 18, dog1)
p1.walk_the_dog()
print(p1.dog.age)

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

from math import pi

class Circle:
    """
    圆的一个类
    radius:半径
    area:求圆的面积
    perimeter:求圆的周长
    """
    def __init__(self, radius):
        self.radius = radius

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

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


circle1 = Circle(5)
print(circle1.area())
print(circle1.perimeter())

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

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

class Student:
    __slots__ = ('name', 'age', 'study_id')

    def __init__(self, name, age, study_id):
        self.name = name
        self.age = age
        self.study_id = study_id

    def massage(self):
        print('到,我叫%s,今年%d岁,学号是%s' % (self.name, self.age, self.study_id))


class Class:

    def __init__(self, class_name, *students):
        self.name = class_name
        self.students = list(students)

    def add_student(self, student):  # 添加学生
        self.students.append(student)
        return

    def del_student(self, student):  # 删除学生
        self.students.remove(student)

    def call_the_roll(self):  # 点名
        for student in self.students:
            print(student.name)
        return

    def ave_age(self):  # 平均年龄
        sum_age = 0
        for student in self.students:
            sum_age += student.age
        return sum_age/len(self.students)


if __name__ == '__main__':
    s1 = Student('小明', 18, '001')
    s2 = Student('小画', 13, '002')
    s3 = Student('小度', 21, '003')
    class1 = Class('py1904', s1)
    class1.add_student(s2)
    class1.add_student(s3)
    class1.add_student(Person('小张', 17, '004'))
    class1.call_the_roll()
    print(class1.ave_age())

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