美文网首页
Day13--课后作业

Day13--课后作业

作者: zxhlcl | 来源:发表于2018-10-17 19:23 被阅读0次
    1. 声明一个电脑类
      属性:品牌、颜色、内存大小
      方法:打游戏、写代码、看视频
      a.创建电脑类的对象,然后通过对象点的方式获取、修改、添加和删除它的属性
      b.通过attr相关方法去获取、修改、添加和删除它的属性
    
    class Computer:
        def __init__(self,brand,color,ram):
            self.brand=brand
            self.color=color
            self.ram=ram
    
        def play_game(self):
            print("play game")
    
        def write_code(self):
            print("write code")
    
        def view_video(self):
            print("view video")
    
        def __str__(self):
            return str(self.__dict__)
    
    computer_one=Computer('Acer','black','4G')
    #a.对象点的方式获取、修改、添加和删除它的属性
    print(computer_one) #{'brand': 'Acer', 'color': 'black', 'ram': '4G'}
    computer_one.brand="Lenovo"
    computer_one.color="yellow"
    computer_one.ram="8G"
    print(computer_one) #{'brand': 'Lenovo', 'color': 'yellow', 'ram': '8G'}
    computer_one.storage="1T"
    print(computer_one) #{'brand': 'Lenovo', 'color': 'yellow', 'ram': '8G', 'storage': '1T'}
    del computer_one.color
    print(computer_one) #{'brand': 'Lenovo', 'ram': '8G', 'storage': '1T'}
    #b.通过attr相关方法去获取、修改、添加和删除它的属性
    computer_two=Computer('Acer','black','4G')
    a=getattr(computer_two,"brand")
    b=getattr(computer_two,"color")
    c=getattr(computer_two,"ram")
    print(a,b,c) #Acer black 4G
    setattr(computer_two,"brand","Lenovo")
    setattr(computer_two,"color","blue")
    setattr(computer_two,"ram","8G")
    print(computer_two) #{'brand': 'Lenovo', 'color': 'blue', 'ram': '8G'}
    delattr(computer_two,'brand')
    print(computer_two)#{'color': 'blue', 'ram': '8G'}
    setattr(computer_two,'strorage','2T')
    print(computer_two)#{'color': 'blue', 'ram': '8G', 'strorage': '2T'}
    

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

    class Dog:
    
        def __init__(self,name,age,color):
            self.name=name
            self.age=age
            self.color=color
    
        def barking(self):
            print("Dog barking")
    
    
    class Person:
    
        def __init__(self,name,age,dog):
            self.name=name
            self.age=age
            self.dog=dog
    
        def walking_the_dog(self,dog):
            print("%s 正在遛一只名字为%s,颜色为%s的狗"% (self.name,dog.name,dog.color))
            dog.barking()
    
    
    dog1=Dog("沙皮",2,"黄色")
    p1=Person("小明",20,dog1)
    p1.walking_the_dog(dog1)
    '''
    小明 正在遛一只名字为沙皮,颜色为黄色的狗
    Dog barking
    '''
    

    3.声明一个矩形类:
    属性: 长、宽
    方法:计算周长和面积
    a.创建不同的矩形,并且打印其周长和面积

    class Rectangle:
        def __init__(self,length,width):
            self.length=length
            self.width=width
    
        def perimeter(self):
            return (self.length + self.width)*2
    
        def area(self):
            return self.length*self.width
    
    rec1=Rectangle(20,30)
    print(rec1.perimeter(),rec1.area())
    rec2=Rectangle(10,20)
    print(rec2.perimeter(),rec2.area())
    

    4.创建一个学生类:
    属性:姓名,年龄,学号,成绩
    方法:答到,展示学生信息
    创建一个班级类: 属性:学生,班级名
    方法:添加学生,删除学生,点名, 获取班级中所有学生的平均值, 获取班级中成绩最好的学生

    class Student:
        def __init__(self, name, age, id,score):
            self.name = name
            self.age = age
            self.id = id
            self.score=score
    
        def answer(self):
            print(self.__dict__)
    
        def __str__(self):
            return str(self.__dict__)
    
    class Class:
        def __init__(self, name, students:list):
            self.name = name
            self.students = students
    
        def add_student(self,stu:Student):
            self.students.append(stu)
    
        def del_student(self,stu:Student):
            self.students.remove(stu)
    
        def call_student(self,stu:Student):
            stu.answer()
    
    
        def average_score(self):
            sum=0
            average=0
            for stu in self.students:
                sum+=stu.score
            average=sum/len(self.students)
            return average
    
        def best_student(self):
            students.sort(key= lambda item:item.score,reverse=True)
            return students[0].name
    
        def __str__(self):
            return str(self.__dict__)
    
    stu1 = Student("小明", 22, "stu01", 98)
    stu2 = Student("小李",24,"stu02",89)
    stu3 = Student("小赵",28,"stu03",78)
    stu4 = Student("小张",26,"stu04",93)
    stu5 = Student("小王",22,"stu05",86)
    stu6=Student("小钱",26,"stu05",95)
    students=[stu1,stu2,stu3,stu4,stu5]
    python_class=Class("python1807",students)
    print(python_class)
    stu6=Student("小钱",26,"stu05",95)
    #添加学生
    python_class.add_student(stu6)
    print(python_class)
    #删除学生
    python_class.del_student(stu6)
    print(python_class)
    #求平均成绩
    print(python_class.average_score())
    #最优秀的学生
    print(python_class.best_student())
    #点名
    python_class.call_student(stu1)
    '''
    结果为:
    {'name': 'python1807', 'students': [<__main__.Student object at 0x000002421A404B00>, <__main__.Student object at 0x000002421A404B38>, <__main__.Student object at 0x000002421A404B70>, <__main__.Student object at 0x000002421A404BA8>, <__main__.Student object at 0x000002421A404BE0>]}
    {'name': 'python1807', 'students': [<__main__.Student object at 0x000002421A404B00>, <__main__.Student object at 0x000002421A404B38>, <__main__.Student object at 0x000002421A404B70>, <__main__.Student object at 0x000002421A404BA8>, <__main__.Student object at 0x000002421A404BE0>, <__main__.Student object at 0x000002421A404D68>]}
    {'name': 'python1807', 'students': [<__main__.Student object at 0x000002421A404B00>, <__main__.Student object at 0x000002421A404B38>, <__main__.Student object at 0x000002421A404B70>, <__main__.Student object at 0x000002421A404BA8>, <__main__.Student object at 0x000002421A404BE0>]}
    88.8
    小明
    {'name': '小明', 'age': 22, 'id': 'stu01', 'score': 98}
    
    '''
    

    方法二:

    import  random
    class Student:
        
        gen_state=(x % 2 for x in range(1,101) )  #状态生成器
        gen_id=("python"+str(x).rjust(3,"0") for x in range(1,101)) #学号生成器
        
        def __init__(self, name, age):
            self.name = name
            self.age = age
            self.id = self.__class__.gen_id.__next__()
            self.score=random.randint(1, 101)
            self.state=self.__class__.gen_state.__next__()
          
        def answer(self):
            if self.state:
                print("%s 到!"% self.name)
            else:
                print("%s 缺席!"% self.name)
    
        def show_info(self):
            print(self.__dict__)
    
        def __repr__(self):
            return str(self.__dict__)
    
    class PythonClass:
    
        def __init__(self, name, students: list):
            self.name = name
            self.students = students
    
        def add_student(self):
            name=input('姓名:')
            age = input("年龄:")
            stu=Student(name,age)
            self.students.append(stu)
            print("添加成功:",end="")
            stu.show_info()
            return stu
    
        def del_student(self):
            del_name=input("姓名:")
            lenth=len(self.students.copy())
            for stu in self.students.copy():#若不是副本,元素在减少,删除不完
                if stu.name==del_name:
                    self.students.remove(stu)
            if lenth!=len(self.students):
                print("删除成功")
    
        def call_student(self):
            for stu in self.students:
                stu.answer()
    
        def average_score(self):
            sum = 0
            average = 0
            for stu in self.students:
                sum += stu.score
            average = sum / len(self.students)
            return average
    
        def best_student(self):
            self.students.sort(key=lambda item: item.score, reverse=True)
            return self.students[0].name
    
    python=PythonClass("Python1807",[])
    
    stu1=python.add_student()
    stu2=python.add_student()
    stu3=python.add_student()
    stu4=python.add_student()
    
    python.call_student()
    print(python.best_student())
    """
    结果为:
    姓名:xiaoming
    年龄:22
    添加成功:{'name': 'xiaoming', 'age': '22', 'id': 'Python001', 'score': 66, 'state': 1}
    姓名:xiaohuang
    年龄:24
    添加成功:{'name': 'xiaohuang', 'age': '24', 'id': 'Python002', 'score': 94, 'state': 0}
    姓名:xiaozhang
    年龄:25
    添加成功:{'name': 'xiaozhang', 'age': '25', 'id': 'Python003', 'score': 22, 'state': 1}
    姓名:xiaohong
    年龄:27
    添加成功:{'name': 'xiaohong', 'age': '27', 'id': 'Python004', 'score': 9, 'state': 0}
    xiaoming 到!
    xiaohuang 缺席!
    xiaozhang 到!
    xiaohong 缺席!
    xiaohuang
    """
    

    相关文章

      网友评论

          本文标题:Day13--课后作业

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