day14-作业
1. 声明⼀个电脑类: 属性:品牌、颜色、内存大小 方法:打游戏、写代码、看视频,a.创建电脑类的对象,然后通过对象点的方式获取、修改、添加和删除它的属性,b.通过attr相关方法去获取、修改、添加和删除它的属性
class Computer:
def __init__(self, brand, color, memory):
self.brand = brand
self.color = color
self.memory = memory
def games(self):
return '%s适合打游戏' % self.brand
def code(self):
return '%s适合写代码' % self.brand
def video(self):
return '%s适合看视频' % self.brand
# a.
computer1 = Computer('神舟', '黑色', '32G')
# 获取
print(computer1.brand)
# 修改
computer1.color = '白色'
print(computer1.color)
# 添加
computer1.price = 7000
print(computer1.price)
# 删除
del computer1.memory
# b.
computer2 = Computer('苹果', '白色', '256G')
# 获取
print(getattr(computer2, 'brand'))
# 修改
setattr(computer2, 'color', '黑色')
print(computer2.color)
# 添加
setattr(computer2, 'price', 8900)
print(computer2.price)
# 删除
delattr(computer2, 'memory')
2. 声明一个人的类和狗的类:狗的属性:名字、颜色、年龄;狗的方法:叫唤;人的属性:名字、年龄、狗;人的方法:遛狗;a.创建人的对象小明,让他拥有一条狗大黄,然后让小明去遛大黄
class Dog:
def __init__(self, name, color, age):
self.name = name
self.color = color
self.age = age
def shout(self):
return '%s在叫你' % self.name
class Person:
def __init__(self, name, age, dog):
self.name = name
self.age = age
self.dog = Dog(dog.name, dog.color, dog.age)
def walk_dog(self):
return '%s遛%s' % (self.name, self.dog.name)
p1 = Person('小明', 22, Dog('大黄', '黄色', 22))
print(p1.walk_dog())
3. 声明一个圆类,自己确定有哪些属性和方法
class Circle:
pai = 3.14
def __init__(self, rad):
self.radius = rad
# 计算圆的面积
def area(self):
return '面积:%.2f' % (self.pai * self.radius ** 2)
# 计算圆的周长
def girth(self):
return '周长:%.2f' % (2 * self.radius * self.pai)
c1 = Circle(10)
print(c1.girth())
print(c1.area())
4. 创建一个学生类:属性:姓名,年龄,学号;方法:答到,展示学生信息;创建一个班级类:属性:学生,班级名;方法:添加学生,删除学生,点名, 求班上学生的平均年龄
class Students:
count = 0
def __init__(self, name, age):
self.count += 1
self.name = name
self.age = age
self.study_id = 'stu' + str(self.count).zfill(3)
# 答到
@staticmethod
def answer(name):
return '%s到' % name
# 展示学生信息
def stu_print(self):
return '<name:%s,age:%d,study_id:%s>' % \
(self.name, self.age, self.study_id)
class Classes:
classes = []
def __init__(self, stu, name):
# self.students = Students(stu.name, stu.age)
self.student = {'name': stu.name, 'age': stu.age, 'study_id': stu.study_id}
self.name = name
self.classes.append(self.student)
# 添加学生
def add_stu(self, stu):
self.student = {'name': stu.name, 'age': stu.age, 'study_id': stu.study_id}
self.classes.append(self.student)
# 删除学生
def del_stu(self, name):
for stu in self.classes:
if name == stu['name']:
self.student = stu
self.classes.remove(self.student)
break
else:
print('没有该学生')
# 点名
def call_name(self, name):
for stu1 in self.classes:
print('========', stu1)
if name == stu1['name']:
# self.student = name
return Students.answer(name)
else:
return '没有该学生'
# 求平均年龄
def avg_age(self):
sum1 = 0
count = 0
for stu in self.classes:
sum1 += stu['age']
count += 1
return int(sum1 / count)
# 创建班级对象
class1 = Classes(Students('小明', 22), 'python')
# 添加学生
class1.add_stu(Students('小花', 18))
print(class1.classes)
# 求平均年龄
print(class1.avg_age())
# 删除学生
class1.del_stu('小花')
print(class1.classes)
# 点名
print(class1.call_name('小明'))
# print(class1.student.stu_print())