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()
网友评论