2019-11-22类作业

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

class computer:
    def __init__(self,brand = '华硕',color = 'red',DRMA = '8G'):
        self.brand = brand
        self.color = color
        self.DRMA = DRMA

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

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

    def watchMovie(self):
        print('看电影')

computer1 = computer()
print(computer1.color,computer1.brand,computer1.DRMA)
computer1.DRMA = '16G'
computer1.color = 'blue'
computer1.brand = '雷神'
print(computer1.color,computer1.brand,computer1.DRMA)
computer2 = computer()
setattr(computer2,'DRMA','32g')
setattr(computer2,'color','white')
setattr(computer2,'brand','三星')
setattr(computer2,'显卡','gtx860')
print(computer2.color,computer2.brand,computer2.DRMA,computer2.显卡)
delattr(computer2,'显卡')
print(computer2.color,computer2.brand,computer2.DRMA)

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

class Dog:
    def __init__(self,name,color,age):
        self.name = name
        self.color = color
        self.age = age
    def wang(self):
        print('{}在狂吠'.format(self.name))

dog = Dog('大黄黄','yellow',3)
xiaoming = Person('小明',20,dog.name)
xiaoming.walk_dog()

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

class Circle:
    def __init__(self,r,c):
        self.c = c
        self.r = r
    def get_area(self):
        return 3.14*self.r*self.r
c1 = Circle(2,12.56)
print(c1.get_area())

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

class Student:
    def __init__(self,name,age,num):
        self.name = name
        self.age = age
        self.num = num
    def answer(self):
        print('到!!\n我是{},今年{}岁,我的学号是{}'.format(self.name,self.age,self.num))

stu1 = Student('张三',18,190601)
stu1.answer()

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

import random
class Class:
    def __init__(self,students,c_name):
        self.students = students
        self.c_name = c_name

    def add_stu(self,stu):
        self.students.append(stu)
        return self.students
    def del_stu(self,stu):
        del self.students[stu]
    def check_stu(self,stu):
        # stu_x = random.sample(self.students,1)#随机选取学生点名
        print('现在开始点名\n{}'.format(stu.name))
        # print(stu.name)
        # if stu.name == stu_x[0].name:
        #     stu.answer()
        # else:
        #     print('')
    def ava_age(self):
        count_age = 0
        for stu in self.students:
            count_age+=stu.age
        return count_age/len(self.students)

stu2 = Student('李四',20,190603)
stu3 = Student('王二',16,190604)
python = Class([],'python')
python.add_stu(stu1)
python.add_stu(stu2)
python.add_stu(stu3)
python.check_stu(stu1)
stu1.answer()
print(python.ava_age())

你可能感兴趣的:(2019-11-22类作业)