2018-10-17类的作业

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

class Computer:
    """创建一个电脑类"""
    __slots__ = ('brand', 'color', 'rom', 'cpu', 'gpu', 'size', 'prize', 'heavy')
    def __init__(self, brand, color, rom):
        self.brand = brand
        self.color = color
        self.rom = rom

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

    def coding(self):
        print('写代码!')

    def watch_video(self):
        print('看视频!')


ge60 = Computer('微星', 'black', '8GB')
ge60.brand = 'Alien'
print(ge60.brand)  # Alien
my_rom = ge60.rom
print(my_rom)  # 8GB
ge60.gpu = 'GTX760m'
print(ge60.gpu)  # GTX760m
del ge60.gpu
# print(ge60.gpu)  # AttributeError: gpu
# 查找
my_brand = getattr(ge60, 'brand')
print(my_brand)  # Alien
my_color = ge60.__getattribute__('color')
print(my_color)  # black
my_cpu = getattr(ge60, 'cpu', '该属性不存在')
print(my_cpu)  # 该属性不存在
# 添加
setattr(ge60, 'cpu', 'i5')
print(getattr(ge60, 'cpu'))  # i5

ge60.__setattr__('gpu', '1080Ti')
print(ge60.__getattribute__('gpu'))  # 1080Ti

# 修改
setattr(ge60, 'cpu', 'i7')
print(getattr(ge60, 'cpu'))  # i7
ge60.__setattr__('gpu', '1060Ti')
print(ge60.__getattribute__('gpu'))  # 1060Ti

# 删除
delattr(ge60, 'cpu')
print(getattr(ge60, 'cpu', '该属性不存在'))  # 该属性不存在
ge60.__delattr__('gpu')
print(getattr(ge60, 'gpu', '该属性不存在'))  # 该属性不存在

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

class Dog:
    """创建一个狗的类"""

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

    def bark(self):
        print('汪汪汪!')


class Person:
    """人"""

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

    def walk_dog(self):
        print('上街溜%s' % self.dog.name)


dog1 = Dog('旺财', '黄色', 2)
person1 = Person('小明', 18, dog1)
person1.walk_dog()  # 上街溜旺财
dog1.bark()  # 汪汪汪!

3.声明一个矩形类:
属性: 长、宽
方法:计算周长和面积
a.创建不同的矩形,并且打印其周长和面积

class Rectangle:
    """矩形"""

    def __init__(self, length=1, width=1):
        self.length = length
        self.width = width

    # 计算周长
    def get_perimeter(self):
        self.perimeter = 2 * self.length + 2 * self.width
        print(self.perimeter)
        return self.perimeter

    # 计算面积
    def get_area(self):
        self.area = self.length * self.width
        print(self.area)
        return self.area


rectangle1 = Rectangle(width=10)
rectangle2 = Rectangle(5)
rectangle3 = Rectangle(2, width=20)

rectangle1.get_perimeter()  # 22
rectangle1.get_area()  # 10

rectangle2.get_perimeter()  # 12
rectangle2.get_area()  # 5

rectangle3.get_perimeter()  # 44
rectangle3.get_area()  # 40

4.创建一个学生类:
属性:姓名,年龄,学号,成绩
方法:答到,展示学生信息
创建一个班级类: 属性:学生,班级名
方法:添加学生,删除学生,点名, 获取班级中所有学生的平均值, 获取班级中成绩最好的学生

class Student:
    """学生"""

    def __init__(self, name, age, stu_id, score):
        self.name = name
        self.age = age
        self.stu_id = stu_id
        self.score = score

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

    def show_info(self):
        print('姓名:%s, 年龄:%s, 学号:%s, 成绩:%s'\
              % (self.name, self.age, self.stu_id, self.score))


class Class:
    """班级"""
    # 创建班级属性,学生用不定长参数方式创建,并且为Student类的对象
    def __init__(self, class_name, *students):
        self.class_name = class_name
        self.students = list(students)

    def add_stu(self, student):
        self.students.append(student)

    def del_stu(self, student):
        self.students.remove(student)

    def check_name(self):
        for stu in self.students:
            print(stu.name + '!')
            stu.check_in()

    def get_average_score(self):
        sum1 = 0
        for stu in self.students:
            sum1 += stu.score
        avg = sum1/len(self.students)
        print(avg)
        return avg

    def get_best_score_stu(self):
        best_score_stu = max(self.students, key=lambda item: item.score)
        best_score_stu.show_info()
        return best_score_stu


stu1 = Student('小明', 20, 'stu001', 90)
stu2 = Student('小花', 18, 'stu002', 88)
stu3 = Student('小张', 22, 'stu003', 95)
stu4 = Student('小李', 21, 'stu004', 87)
class1 = Class('py1807', stu1, stu2, stu3)
class1.check_name()

class1.get_average_score()
class1.get_best_score_stu()
class1.add_stu(stu4)
class1.del_stu(stu3)
class1.check_name()

class1.get_average_score()
class1.get_best_score_stu()

你可能感兴趣的:(2018-10-17类的作业)