1.声明⼀个电脑类: 属性:品牌、颜⾊、内存⼤小 方法:打游戏、写代码、看视频 a.创建电脑类的对象,然后通过对象点的⽅方式获取、修改、添加和删除它的属性 b.通过attr相关⽅方法去获取、修改、添加和删除它的属性
class Computer:
def __init__(self,brand ='苹果',color='黑色',internal_storage='4G'):
self.brand = brand
self.color = color
self.internal_storage = internal_storage
def paly(self):
print('%s可以打游戏' % self)
def write_code(self):
print('%s可以写代码' % self)
def watch_movies(self):
print('%s可以看电影' % self)
computer = Computer()
computer.brand = '联想'
computer.color = '绿色'
computer.internal_storage = '18T'
getattr(computer, 'brand',None)
print(computer.brand)
print(computer.color)
print(computer.internal_storage)
computer.system = 'windows7'
del computer.brand
del computer.color
delattr(computer, 'inter_storage')
delattr(computer, 'system')
2.声明⼀个人的类和狗的类: 狗的属性:名字、颜⾊色、年年龄 狗的⽅方法:叫唤 人的属性:名字、年年龄、狗 人的⽅方法:遛狗 a.创建⼈人的对象⼩小明,让他拥有⼀一条狗⼤大⻩黄,然后让⼩小明去遛⼤大⻩黄
class Dog:
def __init__(self,name1='老王',color1='绿色',age1=19):
self.name = name1
self.color = color1
self.age = age1
def bark(self):
print('%s在叫' % self)
class Human:
def __init__(self,name,dog, age=18):
self.name = name
self.age = age
self.dog = dog
def walk_a_dog(self,):
print('%s在遛%s' % (self.name,self.dog))
person = Human('小明','大黄')
person.walk_a_dog()
3.声明⼀一个圆类:
class Circle:
nun1 = 3.14
def __init__(self,radius: float):
self.radius = radius
self.n = 3.14
def area(self):
return self.n * self.radius ** 2
def circumference(self):
return 2 * self.n * self.radius
def diameter(self):
return 2 * self.radius
circle = Circle(4)
print(circle.area())
4.创建⼀一个学⽣生类: 属性:姓名,年龄,学号 方法:答到,展示学⽣生信息
class Students:
def __init__(self, name, age, id):
self.name = name
self.age = age
self.id = id
def answer(self):
print('到!我是%s今年%d岁学号是%s' % (self.name, self.age, self.id))
stu = Students('小明',18,'stu0001')
stu.answer()
创建⼀一个班级类: 属性:学⽣生,班级名 方法:添加学⽣生,删除学生,点名, 求班上学生的平均年龄
def file_read_json(file_name):
try:
with open('files/'+file_name, 'r', encoding='utf-8') as f:
return json.load(f)
except (FileNotFoundError, IsADirectoryError):
print('%s不存在' % file_name)
def file_write_json(file_name, content):
try:
with open('files/'+file_name, 'w', encoding='utf-8') as f:
return json.dump(content, f)
except(FileNotFoundError, IsADirectoryError):
print('%s不存在' % file_name)
class Student:
def __init__(self, name, age, id1):
self.name = name
self.age = age
self.id = id1
def answer(self):
print('到!')
class Class:
def __init__(self, class_name):
self.class_name = class_name
def add_student(self, student):
# 提出所有信息的列表
information = file_read_json('students_information.json')
information.setdefault(self.class_name, [])
students = information[self.class_name]
students.append(student.__dict__)
information[self.class_name] = students
file_write_json('students_information.json',information)
def del_student(self, student):
information = file_read_json('students_information.json')
students = information[self.class_name]
for item in students:
if item == student.__dict__:
students.remove(student.__dict__)
print('删除的学生为%s%s' % (self.class_name, student.__dict__))
information[self.class_name] = students
result = file_write_json('students_information.json', information)
break
else:
print('没有该学生')
def show_class_information(self, student):
information = file_read_json('students_information.json')
information.setdefault(self.class_name, [])
students = information[self.class_name]
for item in students:
if item == student.__dict__:
student.answer()
break
def average(self):
information = file_read_json('students_information.json')
information.setdefault(self.class_name, [])
students = information[self.class_name]
count = 0
sum = 0
for item in students:
count += 1
sum += item['age']
print('全部学生的平均年龄为:%d' % (sum/count))
stu2 = Student('小明', 1, 'stu0001')
class1 = Class('2班')
class1.add_student(stu2)
class1.del_student(stu2)
class1.show_class_information(stu2)
class1.average()
网友评论