import wm_file_manager
1.声明一个电脑类: 属性:品牌、颜色、内存大小 方法:打游戏、写代码、看视频
a.创建电脑类的对象,然后通过对象点的方式获取、修改、添加和删除它的属性
b.通过attr相关方法去获取、修改、添加和删除它的属性
print('=========第一题==========')
class Computer:
def __init__(self, brand, color, memory):
self.brand = brand
self.color = color
self.memory = memory
def game(self):
print('打游戏')
def write_code(self):
print('写代码')
def watch_video(self):
print('看视频')
print('=======通过对象点的方式获取、修改、添加和删除它的属性=====')
c1 = Computer('dell', 'blue', '256G')
c1.game()
c1.watch_video()
c1.write_code()
brand = c1.brand
color = c1.color
memory = c1.memory
# 修改
c1.color = 'red'
# 增加
c1.size = '12英寸'
size = c1.size
print('电脑属性:%s, %s, %s, %s' % (brand, color, memory, size))
# 删除
del c1.brand
print(c1.color, c1.memory, c1.size)
print('=======通过attr相关方法去获取、修改、添加和删除它的属性=====')
c2 = Computer('dell', 'blue', '256G')
# 获取对象属性
brand = getattr(c2, 'brand', None)
color = getattr(c2, 'color')
print(brand, color)
# 修改和增加
setattr(c2, 'color', 'red')
setattr(c2, 'size', '12英寸')
print(getattr(c2, 'color'), getattr(c2, 'size'))
# 删除
delattr(c2, 'brand')
2.声明一个人的类和狗的类:
狗的属性:名字、颜色、年龄
狗的方法:叫唤
人的属性:名字、年龄、狗
人的方法:遛狗
a.创建人的对象小明,让他拥有一条狗大黄,然后让小明去遛大黄
print('=========第二题==========')
class Human:
def __init__(self, name='', age=0, dog=''):
self.name = name
self.age = age
self.dog = dog
def walk_the_dog(self):
dog1 = Dog(self.dog, '黄色', 2)
print('%s去遛%s了' % (self.name, self.dog))
dog1.cry()
class Dog:
def __init__(self, name='', color='', age=0):
self.name = name
self.color = color
self.age = age
def cry(self):
print('%s兴奋地一直叫唤!' % self.name)
human1 = Human('小明', 18, '大黄')
human1.walk_the_dog()
3.声明一个圆类:
print('=========第三题==========')
class Circle:
def __init__(self, radius=1):
self.radius = radius
def circle(self):
print('我是一个半径是%.1f的圆' % self.radius)
circle1 = Circle(3)
circle1.circle()
4.创建一个学生类:
属性:姓名,年龄,学号
方法:答到,展示学生信息
创建一个班级类:
属性:学生,班级名
方法:添加学生,删除学生,点名, 求班上学生的平均年龄
print('=========第四题==========')
def show_message(dict1: dict):
return 'name: %s, age: %d, id: %s' % (dict1['name'], dict1['age'], dict1['id'])
class Student:
def __init__(self, class_object):
self.class_objcet = class_object
def check_in(self):
self.class_objcet.check_in()
def show_stu_message(self):
self.class_objcet.show_stu_msg()
class Class:
def __init__(self, class_name=''):
self.class_name = class_name
all_students = wm_file_manager.read_json_file('all_students.json')
if not all_students:
all_students = {}
if not all_students.get('students', None):
all_students['students'] = []
students = all_students['students']
self.students = students
def add_student(self):
all_students = wm_file_manager.read_json_file('all_students.json')
if not all_students:
all_students = {}
count = all_students.get('count', 0)
count += 1
name = input('输入姓名:')
age = int(input('输入年龄:'))
id = 'stu' + str(count).rjust(3, '0')
stu2 = {'name': name, 'age': age, 'id': id}
self.students.append(stu2)
all_students['students'] = self.students
all_students['count'] = count
wm_file_manager.write_json_file(all_students, 'all_students.json')
print('添加成功!')
def del_student(self):
all_students = wm_file_manager.read_json_file('all_students.json')
if not all_students:
print('暂无学生信息!请先添加学生!')
return
if not all_students.get('students', None):
print('暂无学生信息!请先添加学生!')
return
students = all_students['students']
print(students)
name = input('请输入要删除学生的姓名:')
del_students = []
for stu in students:
if name == stu['name']:
del_students.append(stu)
if not del_students:
print('没有该学生!')
return
index = 0
for stu in del_students:
print(str(index) + ' ' + show_message(stu))
index += 1
print('1.继续')
print('2.按任意键退出')
choice = input('请选择:')
if choice != '1':
return
index = int(input('选择要删除学生的序号:'))
if index > len(del_students) - 1:
print('没有这个序号!重来吧!')
return
del_stu = del_students[index]
students.remove(del_stu)
print(students)
all_students['students'] = students
print(all_students)
result = wm_file_manager.write_json_file(all_students, 'all_students.json')
if result:
print('删除成功!')
else:
print('删除失败!')
def show_stu_msg(self):
all_students = wm_file_manager.read_json_file('all_students.json')
students = all_students['students']
for stu in students:
print(show_message(stu))
def check_in(self):
all_students = wm_file_manager.read_json_file('all_students.json')
students = all_students['students']
name = input('点名:')
# 这里应该再创建一个文件保存进班的人。
# 如果点的名字本来在班级就不存在,那么提示该人不存在
# 如果点的名字就是班里的同学,那么再来判断这个人在不在
for stu in students:
if name == stu['name']:
print('%s到!' % name)
break
else:
print('%s不在!' % name)
def average_age(self):
all_students = wm_file_manager.read_json_file('all_students.json')
students = all_students['students']
age = 0
for stu in students:
age += stu['age']
average_age = int(age/len(students))
return average_age
class1 = Class('1班')
# 增删学生信息
# class1.add_student()
# class1.del_student()
# 查看学生信息
stu1 = Student(class1)
stu1.show_stu_message()
# 点名答到
# class1.check_in()
# stu1.check_in()
# 求班上学生的平均年龄
print(class1.average_age())