1.声明⼀个电脑类: 属性:品牌、颜⾊、内存⼤小 方法:打游戏、写代码、看视频 。
a.创建电脑类的对象,然后通过对象.的方式获取、修改、添加和删除它的属性
b.通过attr相关⽅方法去获取、修改、添加和删除它的属性
class Computer:
def __init__(self, brand1, color1,memorySize1):
self.brand = brand1
self.color = color1
self.memorySize = memorySize1
def playing_games(self):
print('%s电脑适合打游戏' % self.brand)
def writing_codes(self):
print('%s电脑适合写代码' % self.brand)
def watching_videos(self):
print('%s电脑适合看视频' % self.brand)
# a.创建电脑类的对象,然后通过对象.的方式获取、修改、添加和删除它的属性
computer1 = Computer('ThinkPad','黑色','8g')
print(computer1.brand) # 查
computer1.brand = '外星人' # 改
computer1.playing_games()
computer1.price = '10000元' # 增
print(computer1.price)
del computer1.price # 删
# print(computer1.price) AttributeError: 'Computer' object has no attribute 'price'
# b.通过attr相关⽅方法去获取、修改、添加和删除它的属性
computer2 = Computer('Apple', '银色', '16g')
computer2.writing_codes()
print(getattr(computer2, 'color')) # 查
print(getattr(computer2, 'price', '无价格'))
setattr(computer2, 'color','金色') # 改
print(computer2.color)
setattr(computer2, 'price','8000元') # 增
print(computer2.price)
delattr(computer2,'price')
# print(computer2.price) AttributeError: 'Computer' object has no attribute 'price'
2.声明⼀个人的类和狗的类:
狗的属性:名字、颜色、年龄
狗的方法:叫唤
人的属性:名字、年龄、狗
人的⽅方法:遛狗
a.创建人的对象小明,让他拥有一条狗大黄,然后让小明去遛大黄
class Human:
def __init__(self,name1,age1,dog1):
self.name = name1
self.age = age1
self.dog = dog1
def walk_dog(self):
print('%s牵着%s在公园散步' % (self.name, self.dog))
class Dog:
def __init__(self, name1,color1,age1):
self.name = name1
self.color = color1
self.age = age1
def bark(self):
print('%s在乱叫' % self.name)
human1 = Human('小明', '18', '大黄')
dog1 = Dog('大黄','黄色', '3')
human1.walk_dog()
3.声明⼀一个圆类,自己确定有哪些属性和方法
import math
class Circle:
# 定义属性:半径和圆心角
def __init__(self,radius, angle):
self.radius = radius
self.angle = angle
# 函数:求圆的周长
def perimeter(self):
return round(2 * math.pi * self.radius,2)
# 函数:求圆的面积
def area(self):
return round(math.pi * self.radius ** 2,2)
# 函数:求弧长
def length(self):
return round(math.pi * self.angle * self.radius / 180,2)
circle1 = Circle(10, 360)
print(circle1.perimeter())
print(circle1.area())
print(circle1.length())
4.创建一个学生类:
属性:姓名,年龄,学号
方法:答到,展示学⽣生信息
创建一个班级类:
属性:学生,班级名
方法:添加学生,删除学生,点名, 求班上学生的平均年龄
import json
class Students:
def __init__(self, name, age=0, stu_id=1):
self.name = name
self.age = age
self.stu_id = stu_id
# 答到
def achieve(self):
print('%s到' % self.name)
# 获取学生信息,生成字典
def information(self):
stu_dict = {'姓名':self.name, '年龄':self.age, '学号':self.stu_id}
return stu_dict
class Class_stu:
def __init__(self, students,class_name):
self.students = students
self.class_name = class_name
# 生成器生成学号,通过列表的长度来确定每次执行程序时学号的初始值
self.stu_id = ('stu' + str(num).zfill(3) for num in range(len(students)+1,100))
# 添加学生
def add_stu(self):
while True:
print('1.添加学生\n2.返回')
value = input('请选择(1-2)')
if value == '1':
stu_name = input('输入姓名')
stu_age = input('输入年龄')
stu_id = next(self.stu_id) # 生成器获取学号
stu = Students(stu_name,stu_age,stu_id).information() # 调用Students类中的方法来保存学生信息
self.students.append(stu) # 将学生信息添加至列表
else:
break
with open('test.json', 'w',encoding='utf-8') as f:
f.write(json.dumps(self.students)) # 将学生信息写入文件
# 删除学生
def delete_stu(self):
name1 = input('请输入要删除的学生名字:')
for i in students:
if i['姓名'] == name1:
students.remove(i)
break
else:
print('无此人')
for j in range(len(students)):
students[j]['学号'] = 'stu' + str(j+1).zfill(3) # 重新生成学号
with open('test.json', 'w',encoding='utf-8') as f:
f.write(json.dumps(self.students)) # 重新写入学生信息
# 点名
def call_stu(self):
name1 = input('请输入要点的学生名字:')
for i in students:
if i['姓名'] == name1:
Students(name1).achieve() # 调用Students类中的方法
break
else:
print('无此人')
# 求平均年龄
def ave_age(self):
sum1 = 0
for i in students:
sum1 += int(i['年龄'])
aver = round(sum1 / len(students),2)
print('平均年龄为:',aver)
# 读取主界面
def show_mainpage():
with open('test.txt', encoding='utf-8') as f:
page = f.read()
print(page)
# 主程序
with open('test.json',encoding='utf-8') as f:
students = json.loads(f.read())
c1 = Class_stu(students,'py1902')
while True:
show_mainpage()
value = input('请选择(1-5)')
if value == '1':
c1.add_stu()
elif value == '2':
c1.delete_stu()
elif value == '3':
c1.call_stu()
elif value == '4':
c1.ave_age()
else:
break