2018-10-17 Day 14 作业

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


class Computer:

    def __init__(self, brand='Asus', color='blank', memory_size='8G'):

        self.brand = brand
        self.color = color
        self.memory_size = memory_size

    def play_com_game(self):
        print('正在用%s的%s的%s电脑在打游戏' % (self.color, self.memory_size, self.brand))

    def write_code(self):
        print('正在用%s的%s的%s电脑在写代码' % (self.color, self.memory_size, self.brand))

    def watch_video(self):
        print('正在用%s的%s的%s电脑在看视频' % (self.color, self.memory_size, self.brand))

    def __str__(self):
        return 'brand: ' + self.brand + 'color: ' + self.color + 'm_size: ' + self.memory_size

# a.创建电脑类的对象,然后通过对象点的方式获取、修改、添加和删除它的属性
computer1 = Computer()

# 获取属性
print('brand:', computer1.brand, 'color:', computer1.color, 'm_size:', computer1.memory_size)

# 修改属性
computer1.brand = 'Think_pad'
computer1.color = 'white'
computer1.memory_size = '16G'
print('brand:', computer1.brand, 'color:', computer1.color, 'm_size:', computer1.memory_size)

# 添加属性
computer1.thickness = "100mm"
print('thickness:', computer1.thickness)

# 删除属性
del computer1.thickness
print(computer1)

# b.通过attr相关方法去获取、修改、添加和删除它的属性
# 获取属性
print(getattr(computer1, 'brand'), getattr(computer1, 'color'), getattr(computer1, 'memory_size'))

# 修改属性
setattr(computer1, 'brand', 'Alien')
setattr(computer1, 'color', 'gray')
setattr(computer1, 'memory_size', '32G')
print(getattr(computer1, 'brand'), getattr(computer1, 'color'), getattr(computer1, 'memory_size'))

# 添加属性
setattr(computer1, 'age', '18')
setattr(computer1, 'thickness', '100mm')
print(getattr(computer1, 'brand'), getattr(computer1, 'color'), getattr(computer1, 'memory_size'), computer1.thickness, computer1.age)

# 删除属性
delattr(computer1, 'age')
delattr(computer1, 'thickness')


# 2.声明一个人的类和狗的类:
# 狗的属性:名字、颜色、年龄
# 狗的 法:叫唤
# 人的属性:名字、 年龄、狗
# 人的方法:遛狗


class Dog:

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

    def bark(self):
        print("%s正在叫唤" % self.name)


class Man:

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

    def took_the_dog(self):
        print("%s正在遛%s" % (self.name, dog.name))

# a.创建人的对象名字叫小明,让他拥有一条狗  ,然后让小明去遛狗
dog = Dog('阿黄', '黄色', '2')
person = Man('小明', '18', dog.bark())
person.took_the_dog()


# 3.声明一个矩形类:
# 属性: 长、宽
# 方法:计算周长和面积


class Rectangle:
    """
    矩形类
    """
    def __init__(self, length, width):
        self.length = length
        self.width = width

    def calc_perimeter(self):
        """求矩形周长"""
        length = self.length
        width = self.width
        perimeter = (length + width) * 2
        print(perimeter)

    def calc_area(self):
        """
        求矩形面积
        :return:面积
        """
        length = self.length
        width = self.width
        area = length * width
        print(area)

# a.创建不同的矩形,并且打印其周长和面积
rectangle1 = Rectangle(2, 5)
rectangle2 = Rectangle(4, 5)

rectangle1.calc_perimeter()
rectangle1.calc_area()

rectangle2.calc_perimeter()
rectangle2.calc_area()


# 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 answer(self):
        print('%s: 到' % self.name)

    def show_stu_info(self):
        print('姓名:' + self.name + ' 年龄:' + self.age + ' 学号:' + self.stu_id + ' 成绩:' + str(self.score))


stu1 = Student('小明', '20', '001', 100)
stu1.show_stu_info()


class Class:
    """
    班级类
    功能:添加学生,删除学生,点名,获取班中所有学生的平均值,获取班中成绩最好的学生
    """
    def __init__(self, team, students=[]):
        self.students = students
        self.team = team

    stu = ''

    def add_stu(self, name, age, stu_id, score):
        stu = Student(name, age, stu_id, score)
        self.students.append(stu)

    def delete_stu(self, name):
        for stu in self.students:
            if name == stu.name:
                self.students.remove(stu)
                return
            else:
                print('没有此学生')
                return

    def average(self):
        sum = 0
        for stu in self.students:
            sum += stu.score
        average_num = sum/len(self.students)
        print(average_num)

    def max_num(self):
        max1 = self.students[0].score
        name = self.students[0].name
        for num in self.students:
            if num.score > max1:
                max1 = num.score
                name = num.name
        print(max1, name)

    def call_name(self, name):
        for name_x in self.students:
            if name_x.name == name:
                print("%s在" % name)
                return
            else:
                print("此班级没有%s" % name)
                return





c1 = Class(1)
c1.add_stu("小王", 20, 222, 100)
c1.add_stu("小明", 30, 33, 90)
c1.average()
c1.max_num()
c1.call_name('小王')
c1.delete_stu('小王')
c1.call_name('小王')



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