day13作业

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

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


    def play_game(self):
        return '打游戏'


    def coding(self):
        return '写代码'


    def watch_TV(self):
        return '看电视'


cp = Computer('ASUS', '黑色', '8G')
cp2 = Computer('hp', '白色', '16G')


#通过对象点的⽅方式获取、修改、添加和删除它的属性
#1)查
print(cp.brand)
print(cp.color)
print(cp.memory)


#2)添加、修改
cp.price = 8000
print(cp.price)
cp.memory = '16G'
print(cp.memory)


#3) 删
del cp.color

# 通过attr相关⽅方法去获取、修改、添加和删除它的属性
#1)查
print(getattr(cp2,'memory'))
print(getattr(cp2,'name1','无名电脑'))

#2)添加、修改
setattr(cp2, 'price', 10000)
setattr(cp2, 'brand', 'lenovo')
print(cp2.price)
print(cp2.brand)

#3) 删
delattr(cp2,'color')


'''
2.声明⼀个人的类和狗的类:  
狗的属性:名字、颜⾊色、年年龄   
狗的⽅方法:叫唤     
人的属性:名字、年年龄、狗   
人的⽅方法:遛狗    
a.创建⼈人的对象⼩小明,让他拥有⼀一条狗⼤大⻩黄,然后让⼩小明去遛⼤大⻩黄  
'''
class Person():
    def __init__(self, name, dog, age=18):
        self.name = name
        self.age = age
        self.dog = dog


    def walk_dog(self):
        return people.name + '遛' + dog1.name1


class Dog():
    def __init__(self, name1, color='yellow', age1=4):
        self.name1 = name1
        self.color = color
        self.age1 = age1


    def cry(self):
        return '叫唤'


dog1 = Dog('大黄')
people = Person('小明', dog1.name1)
print(people.walk_dog())

'''
3.声明⼀一个圆类,自己确定有哪些属性和方法
'''
class Garden():
    def __init__(self, r, pi):
        self.r = r
        self.pi = pi


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


    def girth(self):
        return 2*o.pi*o.r


o = Garden(5, 3.1415926)
print(o.area())
print(o.girth())

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

创建⼀一个班级类:   
属性:学⽣生,班级名   
方法:添加学⽣生,删除学生,点名, 求班上学生的平均年龄
'''
import random
class Student():
    def __init__(self, name, age, num):
        self.name = name
        self.age = age
        self.num = num
        self.state = random.randint(0,1)


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


    def show(self):
        print('姓名:%s 年龄:%d 学号:%s' % (self.name, self.age, self.num))


# student1 = Student('小明', 18, '0001')
# student1.respond()
# student1.show()





class Class():
    def __init__(self, class_id):
        self.students = []
        self.class_id = class_id
        self.nums = ('stu'+str(num).zfill(3) for num in range(50))
    

    def add(self):
        stu_name = input('姓名:')
        stu_age = int(input('年龄:'))
        stu_num = next(self.nums)
        stu = Student(stu_name,stu_age,stu_num)
        return self.students.append(stu)


    def del_student(self):
        del_name = input('删除学生的姓名:')
        for stu in self.students[:]:
            if stu.name == del_name:
                self.students.remove(stu)
                class1.cl_show()

    def call_the_roll(self):
        for stu in self.students:
            print(stu.name)
            if stu.state:
                stu.respond()
            else:
                print('......')

    def average_age(self):
        sum1 = 0
        for stu in self.students:
            sum1 += stu.age
        average = sum1//len(self.students)
        return average

    def cl_show(self):
        for stu in self.students:
            stu.show()


class1 = Class('py1901')


for _ in range(3):
    class1.add()
class1.cl_show()
class1.del_student()
class1.call_the_roll()
print(class1.average_age())

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