美文网首页
Day14-作业

Day14-作业

作者: SheeranED | 来源:发表于2019-06-20 20:29 被阅读0次

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

    class Computer:
        def __init__(self, brand, color, memsize):
            self.brand = brand
            self.color = color
            self.memsize = memsize
    # a.创建电脑类的对象,然后通过对象点的⽅方式获取、修改、添加和删除它的属性
    
    
    c1 = Computer('apple', '银色', '6G')
    print(c1.brand)
    c1.brand = '华硕'
    print(c1.brand)
    c1.screen_size = '15寸'
    print(c1.screen_size)
    del c1.color
    # b.通过attr相关⽅方法去获取、修改、添加和删除它的属性
    c2 = Computer('apple', '银色', '6G')
    print(getattr(c2, 'brand'))
    setattr(c2, 'color', '白色')
    print(c2.color)
    setattr(c2, 'screen_size', '16寸')
    print(c2.screen_size)
    delattr(c2, 'brand')
    
    

    2.声明一个人的类和狗的类:

    狗的属性:名字、颜色、年龄

    狗的方法:叫唤

    人的属性:名字、年龄、狗

    人的方法:遛狗

    a.创建人的对象小明,让他拥有一条狗大黄,然后让小明去遛大黄

    
    class Dog:
        def __init__(self, name, color, age):
            self.name = name
            self.color = color
            self.age = age
    
        def dog_call(self):
            print('%s在叫唤' % self.name)
    
    
    d1 = Dog('大黄', '黄色', '12')
    
    
    class Person:
        def __init__(self, name, age):
            self.name = name
            self.age = age
            self.dog = None
    
        def walk_dog(self):
            if not self.dog:
                print('没有狗')
            else:
                print('%s正在遛%s' % (self.name, self.dog.name))
    
    
    p1 = Person('小明', '25')
    print(d1.name)
    p1.dog = d1  # 让人拥有狗
    print(p1.dog)
    p1.walk_dog()
    
    

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

    
    class Circle:
        pi = 3.14
        def __init__(self, radius):
            self.radius = radius
    
        def area(self):
            return Circle.pi*(self.radius**2)
    
        def perimeter(self):
            return 2*Circle.pi*self.radius
    
    
    c1 = Circle(7)
    print(c1.area())
    print(c1.perimeter())
    
    

    4.创建一个学生类:

    属性:姓名,年龄,学号

    方法:答到,展示学生信息

    创建一个班级类:

    属性:学生,班级名

    方法:添加学生,删除学生,点名, 求班上学生的平均年龄

    class Students:
        def __init__(self, name, age, stu_id):
            self.name = name
            self.age = age
            self.stu_id = stu_id
            self.is_in_class = random.randint(0, 1)
            
        def responses(self):
            print('%s, 到' % self.name)
    
        def show(self):
            print('学生:%s, 年龄:%s, 学号:%s' % (self.name, self.age, self.stu_id))
    
    
    class Class:
        def __init__(self, name):
            self.all_students = []
            self.name = name
            # self.count = 0
    
            def func():
                num = 1
                while True:
                    yield 'stu'+str(num)
                    num += 1
            self.create_id = func()
        def add_student(self):
            name = input('姓名:')
            age = input('年龄:')
            # self.count += 1
            # stu_id = 'stu'+str(self.count).rjust(3, '0')
            stu_id = self.create_id
            stu = Students(name, int(age), stu_id)
            self.all_students.append(stu)
            print('学生:%s, 年龄:%s, 学号:%s' % (name, age, stu_id))
    
        def show_all_students(self):
            for stu in self.all_students:
                stu.show()
    
        def delete(self):
            del_name = input('输入要删除学生姓名:')
            flag = False
            for stu in self.all_students[:]:
                if stu.name == del_name:
                    stu.show()
                    flag = True
                    value = input('是否删除(y/n):')
                    if value == 'y':
                        self.all_students.remove(stu)
                        print('删除成功')
            if not flag:
                print('没有该学生')
    
        def call_name(self):
            for stu in self.all_students:
                print(stu.name)
                if stu.is_in_class:
                    stu.responses()
    
        def average(self):
            sum1 = 0
            for stu in self.all_students:
                sum1 += int(stu.age)
            print('python1903的平均年龄为%s' % (sum1/len(self.all_students)))
    
    
    class1 = Class('python1903')
    for i in range(3):
        class1.add_student()
    class1.delete()
    class1.call_name()
    class1.average()class Students:
        def __init__(self, name, age, stu_id):
            self.name = name
            self.age = age
            self.stu_id = stu_id
            self.is_in_class = random.randint(0, 1)
            
        def responses(self):
            print('%s, 到' % self.name)
    
        def show(self):
            print('学生:%s, 年龄:%s, 学号:%s' % (self.name, self.age, self.stu_id))
    
    
    class Class:
        def __init__(self, name):
            self.all_students = []
            self.name = name
            # self.count = 0
    
            def func():
                num = 1
                while True:
                    yield 'stu'+str(num)
                    num += 1
            self.create_id = func()
        def add_student(self):
            name = input('姓名:')
            age = input('年龄:')
            # self.count += 1
            # stu_id = 'stu'+str(self.count).rjust(3, '0')
            stu_id = self.create_id
            stu = Students(name, int(age), stu_id)
            self.all_students.append(stu)
            print('学生:%s, 年龄:%s, 学号:%s' % (name, age, stu_id))
    
        def show_all_students(self):
            for stu in self.all_students:
                stu.show()
    
        def delete(self):
            del_name = input('输入要删除学生姓名:')
            flag = False
            for stu in self.all_students[:]:
                if stu.name == del_name:
                    stu.show()
                    flag = True
                    value = input('是否删除(y/n):')
                    if value == 'y':
                        self.all_students.remove(stu)
                        print('删除成功')
            if not flag:
                print('没有该学生')
    
        def call_name(self):
            for stu in self.all_students:
                print(stu.name)
                if stu.is_in_class:
                    stu.responses()
    
        def average(self):
            sum1 = 0
            for stu in self.all_students:
                sum1 += int(stu.age)
            print('python1903的平均年龄为%s' % (sum1/len(self.all_students)))
    
    
    class1 = Class('python1903')
    for i in range(3):
        class1.add_student()
    class1.delete()
    class1.call_name()
    class1.average()
    

    相关文章

      网友评论

          本文标题:Day14-作业

          本文链接:https://www.haomeiwen.com/subject/bmhsqctx.html