美文网首页
2019-01-11作业

2019-01-11作业

作者: 百而所思 | 来源:发表于2019-01-11 23:47 被阅读0次

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

    class Computer:
        def __init__(self, brand='None', color='None', memory="None"):
            self.brand = brand
            self.color = color
            self.memory = memory
    
        def play_game(self):
            return "%s的%s%s在打游戏!" % (self.memory, self.color, self.brand)
    
        def write_code(self):
            return "%s的%s%s在敲代码!" % (self.memory, self.color, self.brand)
    
        def watch_videos(self):
            return "%s的%s%s在看视频!" % (self.memory, self.color, self.brand)
    
    
    com = Computer()
    com.brand = 'HP'
    com.memory = '16GB'
    com.color = 'colorful'
    print(com.play_game())
    print(com.watch_videos())
    print(com.write_code())
    delattr(com, 'color')
    print(hasattr(com, 'color'))
    setattr(com, 'color', 'red')
    print(com.color)
    print(getattr(com, 'brand'))
    

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

    class Dog:
        def __init__(self, name='None', color='None', age="None"):
            self.name = name
            self.color = color
            self.age = age
    
        def bark(self):
            return '%s在汪汪汪的大声喊叫' % self.name
    
    
    class Person:
        def __init__(self, name='None', dog: Dog=None, age="None"):
            self.name = name
            self.dog = dog
            self.age = age
    
        def walk_dog(self):
            if self.dog:
                return '%s牵着%s在街上遛弯' % (self.name, self.dog)
            else:
                return '没有狗'
    
    
    d1 = Dog(name='大黄')
    print(d1.bark())
    p1 = Person(name='小明', dog=d1.name)
    print(p1.walk_dog())
    p2 =Person(name='')
    print(p2.walk_dog())
    

    3.声明一个圆类:

    import math
    
    
    class circular():
        def __init__(self, radius):
            self.radius = radius
    
        def area(self):
            return math.pi * self.radius ** 2
    
        def perimeter(self):
            return math.pi * self.radius * 2
    

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

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

    class AllStudent:
        def __init__(self, name='小明', age=0, sid='001'):
            self.name = name
            self.age = age
            self.sid = sid
    
        def sign_in(self):
            return '%s到' % self.name
    
        def show_info(self):
            return self.__dict__
    
    class ClassGrade:
    
        def __init__(self, student=[], class_id=0):
            self.student = student
            self.class_id = class_id
    
        def add_student(self):
            allstdent = AllStudent()
            d1 = {}
            allstdent.name = input('请输入名字:')
            allstdent.age = input('请输入年龄:')
            allstdent.sid = input('请输入学号:')
            self.class_id = input("请输入班级:")
            d1['class_id'] = self.class_id
            d1.update(allstdent.__dict__)
            self.student.append(d1)
    
        def find_student(self):
            allstudent = AllStudent()
            print(self.student)
            n = input('请输入要点的名字:')
            for x in self.student:
                if x['name'] == n:
                    allstudent.name = n
                    print(allstudent.sign_in())
    
        def del_student(self):
            n = input('请输入删除的名字:')
            for x in self.student:
                if x['name'] == n:
                    self.student.remove(x)
    
        def avgAge(self):
            s = 0
            i = 0
            for x in self.student:
                a = int(x['age'])
                s += a
                i += 1
            return s / i
    
    
    c = ClassGrade()
    c.add_student()
    c.find_student()
    c.add_student()
    c.del_student()
    c.add_student()
    print(c.avgAge())
    
    
    

    相关文章

      网友评论

          本文标题:2019-01-11作业

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