day14作业

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

class Computer:
    """类的说明文档:声明的电脑类"""
    __slots__ = ('brand', 'color', 'memory', 'price', 'screen')

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

    def function(self):
        print('%s可以打游戏、敲代码、看视频' % self.brand)


# a.创建电脑类的对象,然后通过对象点的方式获取、修改、添加和删除它的属性
# b.通过attr相关方法去获取、修改、添加和删除它的属性
computer1 = Computer('Asus', '黑色', '4Ghz')
print('++++++++++++++获取+++++++++++++++')
print('品牌:', computer1.brand, '颜色:', computer1.color, '内存:', computer1.memory)  # 点方法获取电脑属性
print('品牌:', getattr(computer1, 'brand'))  # attr 方法获取
print('++++++++++++++修改+++++++++++++++')
computer1.brand = '东芝'
print('品牌:', computer1.brand)  # 点方法修改
setattr(computer1, 'brand', '联想')  # attr方法修改
print('++++++++++++++添加+++++++++++++++')
computer1.price = 5000  # 点方法添加
setattr(computer1, 'screen', '15.4')  # attr 方法添加
print('品牌:', computer1.brand, '颜色:', computer1.color, '内存:', computer1.memory,
      '价格:', computer1.price, '屏幕尺寸:', computer1.screen)
print('++++++++++++++删除+++++++++++++++')
del computer1.brand
delattr(computer1, 'color')
print('内存:', computer1.memory, '价格:', computer1.price, '屏幕尺寸:', computer1.screen)

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

class Person:
    """类的说明:定义人类"""
    def __init__(self, name, dog, age=0):
        self.name = name
        self.age = age
        self.dog = dog

    def playDog(self):
        print('%s有一条叫做%s的狗,小明每天带着%s去遛弯' % (self.name, self.dog, self.dog))


class Dog:
    """类的说明:定义狗类"""
    def __init__(self, name, color='黑色', age=1):
        self.name = name
        self.color = color
        self.age = age

    def bark(self):
        print('%s天天叫唤个不停')


dog1 = Dog('大黄')
p1 = Person('小明', dog1.name)
p1.playDog()

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

class Circle:
    """类的说明:定义一个圆类,并求圆的周长和面积"""
    global pi
    pi = 3.14

    def __init__(self, r):
        self.r = r

    def perimeter(self):
        rerimeter = 2 * pi * self.r
        return rerimeter

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


circle1 = Circle(3)
print('圆的面积为%s' % circle1.perimeter())
print('圆的面积为%s' % circle1.area())

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

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

import random


class Student:
    """类的说明:定义一个学生类"""
    def __init__(self, name, age, stuid='001'):
        self.name = name
        self.age = age
        self.stuid = stuid
        self.state = random.randint(0, 1)

    def answer(self):
        if self.state == 0:
            print('%s没有到' % self.name)
        else:
            print('%s 到了' % self.name, '姓名:%s  年龄:%s  学号:%s' % (self.name, self.age, self.stuid))


class Class:
    """
    类的说明:定义一个班级类
    实现添加学生,删除学生并且求出学生平均年龄的功能
    """

    def __init__(self, class_name, *student):
        self.class_name = class_name
        self.student = list(student)
        # for student in self.student:
        #     print('姓名:%s  年龄:%s  学号:%s' % (student.name, student.age, student.stuid))

    def add_student(self, stu):
        self.student.append(stu)
        # for student in self.student:
        #     print('姓名:%s  年龄:%s  学号:%s' % (student.name, student.age, student.stuid))

    def del_student(self, student):
        self.student.remove(student)
        # for student in self.student:
        #     print('姓名:%s  年龄:%s  学号:%s' % (student.name, student.age, student.stuid))

    def answer_student(self):
        for stu in self.student:
            stu.answer()

    def avg_age(self):
        totals = 0
        for stu in self.student:
            totals += stu.age
        avg_age = totals / len(self.student)
        return avg_age


student1 = Student('学生1', 58, '001')
student2 = Student('学生2', 45, '002')
student3 = Student('学生3', 43, '003')
student4 = Student('学生4', 40, '004')
student5 = Student('学生5', 62, '005')
class1 = Class('python1902', student1, student2, student3, student4)
# print(class1.student)
class1.add_student(student5)  # 添加学生
# print(class1.student)
class1.del_student(student2)  # 删除学生
# print(class1.student)
print('班级学生的平均年龄为:', class1.avg_age())  # 求学生的平均年龄
class1.answer_student()  # 学生点到

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