作业_Day12

2018-10-17  本文已影响0人  龙神海王
  1. 声明一个电脑类
    属性:品牌、颜色、内存大小
    方法:打游戏、写代码、看视频

a.创建电脑类的对象,然后通过对象点的方式获取、修改、添加和删除它的属性
b.通过attr相关方法去获取、修改、添加和删除它的属性




class Computer:
    '''电脑'''
    def __init__(self,brand,color,memory=0):
        self.brand = brand
        self.color = color
        self.memory = memory

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

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

    def sea_video(self):
        print('看视频')

p1 = Computer('huasuo','white','500g')
p2 = Computer('waixingren','black','888g')
print(p1.brand)
p2.memory = '666g'
print(p2.memory)
p1.price = 10000
print(p1.price)
del p1.price
# print(p1.price)

color1 = getattr(p2,'color','blue')
print(color1)
print(p1.brand)
setattr(p1,'brand','lianxiang')
print(p1.brand)
setattr(p2,'system',7)
print(p2.system)
delattr(p1,'memory')

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

class Dog:
    def __init__(self,name,color,age):
        self.name = name
        self.color = color
        self.age = age

    def barks(self):
        print('叫唤')

class Person:
    def __init__(self,name,age,dog:Dog):
        self.name = name
        self.age = age
        self.dog = None

    def walk_dog(self):
        print('遛狗')
p1 = Person('小明',18,Dog('小黑’,'heise',5)
p1.walk_dog()

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

class Rectangular:
    def __init__(self,long = 0,width = 0):
        self.long = long
        self.width = width

    def calculate_perimeter(self,long,width):
        perimeter = (int(long)+int(width))*2
        return perimeter

    def calculate_area(self,long,width):
        area = int(long) * int(width)
        return area
p1 = Rectangular(8,6)
p2 = Rectangular(12,24)
area1 = p1.calculate_area(p1.long,p1.width)
perimeter1 = p1.calculate_perimeter(p1.long,p1.width)    # 求矩形p1的周长和面积
print('p1的周长是%d,面积是%d' % (perimeter1,area1))

area2 = p2.calculate_area(p2.long,p2.width)
perimeter2 = p2.calculate_perimeter(p2.long,p2.width)   # 求矩形p2的周长和面积
print('p2的周长是%d,面积是%d' % (perimeter2,area2))

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

class Students:
    def __init__(self,name,age,study_id,scors):
        self.name = name
        self.age = age
        self.study_id = study_id
        self.scors = scors

    def replied(self):
        print('答道')

    def show_stu_info(self,*p):
       for stu in p:
            stu_info = stu.__dict__
            print(stu_info)



p1 = Students('sea',22,'001',96)
p2 = Students('dragon',21,'002',99)




class Class:
    def __init__(self,students = [],class_name = ''):
        self.students = students
        self.class_name = class_name

    def add_student(self,student):
        p3.students.append(student)
        return p3.students

    def del_student(self,student):
        p3.students.remove(student)
        return p3.students

    def call_roll(self,student):
        print(student.name)


    def average(self,p3):
        sum1 = 0
        for stu in p3.students:
            sum1 += stu.scors
        average = sum1/len(p3.students)
        return average

    def best_scors(self,p):
        max1 = max(p3.students,lambda item:item.scors)
        return max1
p3 = Class([],'python1807')
student = Students('小强',16,'003',77)
上一篇下一篇

猜你喜欢

热点阅读