2019-01-11Day15作业

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

class Computer:
    def __init__(self, brand='Mac', color='silver', rom=8):
        self.brand = brand
        self.color = color
        self.rom = rom

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

    def codeing(self):
        print('写代码')

    def watching(self):
        print('看视频')


c1 = Computer('戴尔', 'black', 8)

print(c1.brand)

c1.color = 'red'

c1.price = 4000

del c1.rom

print(c1.__dict__)

print(getattr(c1, 'brand', '没有该品牌'))

setattr(c1, 'color', 'white')

setattr(c1, 'size', 'small')

delattr(c1, 'price')

print(c1.__dict__)

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

class Dog:
    def __init__(self, name='孙笑川', color='黄色', age=22):
        self.name = name
        self.color = color
        self.age = age

    def call_out(self):
        print('叫唤')


d1 = Dog()


class Person:
    def __init__(self, name='李相赫', age=22, dog=''):
        self.name = name
        self.age = age
        self.dog = dog

    def walk_the_dog(self):
        print('遛狗')
        self.dog.call_out()


p1 = Person('小明', 21, d1)

p1.walk_the_dog()

3.声明⼀一个圆类:

class Circle(object):
    pi = 3.1415926
    def __init__(self, r, x, y):
        self.r = r
        self.x = x
        self.y = y
    def get_d(self):
        return 2*self.r
    def get_area(self):
        return Circle.pi*self.r**2
    def circle_x(self):
        return abs(self.y - self.r)
    def circle_y(self):
        return abs(self.x - self.r)


circle1 = Circle(5, 10, 20)
print(circle1.get_d(), circle1.get_area(), circle1.circle_y(), circle1.circle

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

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



class Class:
    def __init__(self, name, student={}):
        self.name = name
        self.student = student

    def stu_add(self, stu_name, student):
        self.student[stu_name] = student

    def show_stu(self):

        for stu in self.student:
            print(self.student[stu].__dict__)

    def delete_stu(self, stu_name):
        del self.student[stu_name]

    def talk(self, stu):
        stu.speak()

    def max_stu(self, subject):
        max_score = 0
        stu = ''
        for x in self.student:
            if max_score < self.student[x].score[subject]:
                max_score = self.student[x].score[subject]
                stu = x
        return max_score, x

    def average(self, subject):
        all_score = 0
        average_score = 0
        num = 0
        for x in self.student:
            all_score += self.student[x].score[subject]
            num += 1
        average_score = all_score/num
        return average_score

stu1 = Student('孙笑川', 21, 101, math=90)
stu2 = Student('明凯', 22, 102, math=69)
stu3 = Student('李相赫', 22, 103, math=59)

cl1 = Class('学前班')
cl1.stu_add('孙笑川', stu1)
cl1.stu_add('明凯', stu2)
cl1.stu_add('李相赫', stu3)

print(cl1.max_stu('math'), cl1.average('math'))

cl1.show_stu()

stu1.show()

stu1.speak()

你可能感兴趣的:(2019-01-11Day15作业)