Day15-作业

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

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

    @staticmethod
    def play_game():
        print('打游戏')

    @staticmethod
    def write_code():
        print('写代码')

    @staticmethod
    def watch_video():
        print('看视频')


def main():
    print('===============作业第一题==================')
    pc = Computer('苹果', '白色', '16G')
    pc.play_game()
    pc.write_code()
    pc.watch_video()

    print('=====通过对象.的方式去操作对象的属性=====')
    # 获取对象的属性
    print(pc.brand, pc.color, pc.memory)

    # 修改对象的属性
    pc.brand = '联想'
    pc.color = '黑色'
    pc.memory = '8G'
    print(pc.brand, pc.color, pc.memory)

    # 添加对象的属性
    pc.name = '小黑'
    print(pc.brand, pc.color, pc.memory, pc.name)

    # 删除对象的属性
    del pc.brand
    # print(pc.brand, pc.color, pc.memory, pc.name)   # AttributeError: 'Computer' object has no attribute 'brand'
    print(pc.color, pc.memory, pc.name)

    print('=====通过attr相关方法操作对象的属性======')
    # 获取对象的属性
    print(getattr(pc, 'color'))
    print(getattr(pc, 'memory'))
    print(getattr(pc, 'name'))

    # 修改对象的属性
    setattr(pc, 'color', '白色')
    setattr(pc, 'memory', '32G')
    setattr(pc, 'name', '小白')

    print(getattr(pc, 'color'))
    print(getattr(pc, 'memory'))
    print(getattr(pc, 'name'))

    # 添加对象的属性
    setattr(pc, 'brand', '小米')

    print(getattr(pc, 'brand'))

    # 删除对象的属性
    delattr(pc, 'name')

    print(getattr(pc, 'name', None))


if __name__ == '__main__':
    main()

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

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

    def walking_the_dog(self):
        print('%s在遛%s' % (self.name, self.dog))


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

    def call_out(self):
        print('%s在叫唤' % self.name)


def main():
    dog = Dog('大黄', '黄色', 5)
    p1 = Person('小明', 18, dog.name)

    p1.walking_the_dog()
    dog.call_out()


if __name__ == '__main__':
    main()

3.声明⼀个圆类:

class Circle:
    def __init__(self, radius=0):
        self.radius = radius

    def circumference(self):
        return 2 * 3.14 * self.radius

    def area_circle(self):
        return 3.14 * self.radius ** 2


def main():
    circle = Circle(5)
    print('圆的周长为%.2f' % circle.circumference())
    print('圆的面积为%.2f' % circle.area_circle())


if __name__ == '__main__':
    main()

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

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

class Student:
    def __init__(self, name='', age=0, stu_id=''):
        self.name = name
        self.age = age
        self.stu_id = stu_id

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

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


class Clbum:
    def __init__(self, student, class_name=''):
        self.student = student
        self.class_name = class_name

    def add_student(self, stu_dict):
        self.student.append(stu_dict)

    def del_student(self):
        del_list = []
        while True:
            print('1.根据姓名删除学生')
            print('2.根据学号删除学生')
            print('3.退出')
            input_value = input('请选择')
            if input_value == '1':
                del_name = input('请输入要删除的姓名:')
                for stu in self.student:
                    if stu['name'] == del_name:
                        del_list.append(stu)
                index = 0
                for stu in del_list:
                    print(str(index) + ' ' + '姓名:%s 年龄:%s 学号:%s' % (stu['name'], stu['age'], stu['stu_id']))
                    index += 1
                input_value = input('请输入要删除的学生对应的标号:')

                del_stu = del_list[int(input_value)]
                self.student.remove(del_stu)
                print(self.student)
            elif input_value == '2':
                del_id = input('请输入要删除的学号:')
                for stu in self.student:
                    if stu['stu_id'] == del_id:
                        self.student.remove(stu)
                print(self.student)
            elif input_value == '3':
                break
            else:
                print('输入有误。请重新输入!')

    def call_the_roll(self):
        for stu in self.student:
            print('%s' % stu['name'])

    def aver_age(self):
        sum1 = 0
        for stu in self.student:
            sum1 += int(stu['age'])
        return sum1 / len(self.student)


def main():
    stu1 = Student('小明', 18, '1'.rjust(4, '0'))
    stu1.answer()
    stu1.show_student_info()

    grade1 = Clbum([], '一班')
    # 添加学生
    num_people = int(input('请输入学生的人数:'))
    for index in range(1, num_people + 1):
        name = input('请输入学生的姓名:')
        age = input('请输入学生的年龄:')
        stu2 = Student(name, age, str(index).rjust(4, '0'))
        grade1.add_student(stu2.__dict__)
    print(grade1.student)

    # 点到
    grade1.call_the_roll()
    # 计算学生的平均年龄
    aver = grade1.aver_age()
    print('平均年龄为%.2f' % aver)

    # 删除学生
    grade1.del_student()


if __name__ == '__main__':
    main()
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,544评论 6 501
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,430评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,764评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,193评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,216评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,182评论 1 299
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,063评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,917评论 0 274
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,329评论 1 310
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,543评论 2 332
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,722评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,425评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,019评论 3 326
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,671评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,825评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,729评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,614评论 2 353

推荐阅读更多精彩内容