day14-homework

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


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

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

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

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


com1 = Computer('苹果', '黑色', '256GB')
# 获取
print(com1.brand)
# 修改
com1.brand = '联想'
print(com1.brand)
# 添加
com1.card = 'NVIDIA GeForce GTX 850M'
print(com1.card)

# 删除
del com1.color

b.通过attr相关⽅方法去获取、修改、添加和删除它的属性

print('==============================')
com2 = Computer('苹果', '白色', '256GB')
# 获取
print(getattr(com2, 'color'))
# 修改
setattr(com2, 'memory', '125GB')
print(com2.memory)
# 添加
setattr(com2, 'Vcard', 'NVIDIA GeForce RTX 2080Ti Foun')
print(com2.Vcard)
# 删除
delattr(com2, 'color')

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


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

    def call(self):
        print('汪汪汪汪汪汪汪')


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

    def walk_the_dog(self):
        return '%s遛%s' % (self.name, self.dog)


dog1 = Dog('大黄', '黄色', 3)
per1 = Person('小明', 20, dog1.name)
print(per1.walk_the_dog())

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


class Circle:
    tem = 3.14

    def __init__(self, radius):
        self.radius = radius

    # 计算周长
    def perimeter(self):
        return self.radius * self.tem * 2

    # 计算面积
    def area(self):
        return self.radius * self.radius * self.tem


c1 = Circle(3)
print(c1.perimeter())
print(c1.area())
  • 创建一个学生类:
    属性:姓名,年龄,学号
    方法:答到,展示学生信息

class Student:
    dict1 = {}

    def __init__(self, name, age, stu_id):
        self.name = name
        self.dict1['name'] = self.name
        self.age = age
        self.dict1['age'] = self.age
        self.stu_id = stu_id
        self.dict1['stu_id'] = self.stu_id

    def report(self, num):
        if num != 0:
            print('到!!%s的基本信息' % self.name, end='')
            print(self.dict1)
        else:
            print('%s不在' % self.name)


stu1 = Student('小明', 20, '001')
stu1.report(0)
stu1.report(1)
  • 创建一个班级类:
    属性:学生,班级名
    方法:添加学生,删除学生,点名, 求班上学生的平均年龄

class Class1:
    def __init__(self, list1: str, classname='python1902'):
        self.classname = classname
        self.list1 = list1

    def add_student(self, student):
        self.list1.append(student)
        print(self.list1)

    def del_student(self, student):
        self.list1.remove(student)
        print(self.list1)

    def call_student(self):
        for item in self.list1:
            print(item['name'])

    def ave_age(self):
        sum = 0
        num = 0
        for item in self.list1:
            sum += item['age']
            num += 1
        print('平均年龄是:',sum/num)


c1 = Class1([{'name': '小明', 'age': 20, 'stu_id': '001'}])
c1.add_student({'name': '小李', 'age': 22, 'stu_id': '002'})
c1.add_student({'name': '小王', 'age': 20, 'stu_id': '003'})
c1.add_student({'name': '小法', 'age': 26, 'stu_id': '004'})
print(c1.add_student({'name': '小龙', 'age': 24, 'stu_id': '005'}))
print(c1.del_student({'name': '小明', 'age': 20, 'stu_id': '001'}))
print(c1.call_student())
print(c1.ave_age())
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。