1.声明⼀个电脑类: 属性:品牌、颜⾊、内存⼤小 方法:打游戏、写代码、看视频
class Computer:
def __init__(self, brand, color, ram):
self.brand = brand
self.color = color
self.ram = ram
@staticmethod
def play_game():
print('这是在玩游戏')
@staticmethod
def write_code():
print('这是在写代码')
@staticmethod
def see_video():
print('这是在看视频')
a.创建电脑类的对象,然后通过对象点的⽅方式获取、修改、添加和删除它的属性
puter = Computer('联想', '黑色', '8G')
# 获取品牌属性
print(puter.brand)
# 获取颜色属性
print(puter.color)
# 获取内存信息
print(puter.ram)
print(puter.__dict__)
# 修改品牌属性
puter.brand = '戴尔'
# 修改颜色属性
puter.color = '蓝色'
# 修改内存信息
puter.ram = '4G'
print(puter.__dict__)
# 添加属性
puter.size = '14寸'
print(puter.__dict__)
# 删除属性
del puter.size
print(puter.__dict__)
这是结果:
E:\LHYPython\homework\day15_类和对象\venv\Scripts\python.exe
E:/LHYPython/homework/day15_类和对象/01_work.py
联想
黑色
8G
{'brand': '联想', 'color': '黑色', 'ram': '8G'}
{'brand': '戴尔', 'color': '蓝色', 'ram': '4G'}
{'brand': '戴尔', 'color': '蓝色', 'ram': '4G', 'size': '14寸'}
{'brand': '戴尔', 'color': '蓝色', 'ram': '4G'}
Process finished with exit code 0
b.通过attr相关⽅方法去获取、修改、添加和删除它的属性
puter2 = Computer('苹果', '银色', '8G')
# 获取品牌属性
print(getattr(puter2, 'brand', '未知'))
# 获取颜色属性
print(getattr(puter2, 'color', '未知'))
# 获取内存属性
print(getattr(puter2, 'ram', '未知'))
print(puter2.__dict__)
# 修改品牌属性
setattr(puter2, 'brand', '外星人')
# 修改颜色属性
setattr(puter2, 'color', '黑色')
# 修改内存属性
setattr(puter2, 'ram', '12G')
print(puter2.__dict__)
# 添加尺寸属性
setattr(puter2, 'size', '15.6寸')
print(puter2.__dict__)
# 删除尺寸属性
delattr(puter2, 'size')
print(puter2.__dict__)
这是结果:
E:\LHYPython\homework\day15_类和对象\venv\Scripts\python.exe
E:/LHYPython/homework/day15_类和对象/01_work.py
苹果
银色
8G
{'brand': '苹果', 'color': '银色', 'ram': '8G'}
{'brand': '外星人', 'color': '黑色', 'ram': '12G'}
{'brand': '外星人', 'color': '黑色', 'ram': '12G', 'size': '15.6寸'}
{'brand': '外星人', 'color': '黑色', 'ram': '12G'}
Process finished with exit code 0
2.声明⼀个人的类和狗的类:
狗的属性:名字、色、年龄
狗的⽅方法:叫唤
人的属性:名字、年龄、狗
人的方法:遛狗
class Person:
def __init__(self, name, age, dog):
self.name = name
self.age = age
self.dog = dog
def walk_dog(self):
print('{}正在遛着{},'.format(self.name, self.dog), end='')
class Dog:
def __init__(self, name, color, age):
self.name = name
self.color = color
self.age = age
def bark(self):
print('这个时候{}却突然叫唤起来'.format(self.name))
a.创建人的对象小明,让他拥有⼀一条狗⼤黄,然后让小明去遛⼤黄
dog = Dog('大黄', '黄色', 3)
person = Person('小明', 24, dog.name)
person.walk_dog()
dog.bark()
E:\LHYPython\homework\day15_类和对象\venv\Scripts\python.exe
E:/LHYPython/homework/day15_类和对象/02_work.py
小明正在遛着大黄,这个时候大黄却突然叫唤起来
Process finished with exit code 0
3.声明一个圆类:
class Circle:
def __init__(self, r):
self.pi = pi
self.r = r
def area(self):
print('r={}的圆面积是{}'.format(self.r, pi * self.r ** 2))
def perim(self):
print('r={}的圆周长是{}'.format(self.r, pi * self.r * 2))
这是结果:
E:\LHYPython\homework\day15_类和对象\venv\Scripts\python.exe
E:/LHYPython/homework/day15_类和对象/03_work.py
r=10的圆面积是314.1592653589793
r=10的圆周长是62.83185307179586
Process finished with exit code 0
4.创建一个学⽣生类:
属性:姓名,年龄,学号
方法:答到,展示学生信息
class Student:
def __init__(self, ids, name, age):
self.id = ids
self.name = name
self.age = age
def check_in(self):
print('学生{}已到'.format(self.name))
def show_info(self):
info = list(dict(self.__dict__).values())
print('学号:{}, 姓名:{}, 年龄{}'.format(info[0], info[1], info[2]))
这是结果:
E:\LHYPython\homework\day15_类和对象\venv\Scripts\python.exe
E:/LHYPython/homework/day15_类和对象/04_work.py
学生小明已到
学号:stu001, 姓名:小明, 年龄20
Process finished with exit code 0
创建一个班级类:
属性:学生,班级名
方法:添加学生,删除学生,点名, 求班上学生的平均年龄
class ClassGrade:
def __init__(self, student):
self.student = student
self.grade_name = 1
def add_stu(self, num):
while True:
num += 1
stu_id = 'stu' + str(num).rjust(3, '0')
stu_name = input('name:')
stu_age = int(input('age:'))
self.student.append({'id': stu_id, 'name': stu_name, 'age': stu_age})
setattr(self, 'student', self.student)
print('添加成功')
input_value = input('是否继续添加(Y/N)')
if input_value != 'Y' and input_value != 'y':
break
return
def del_stu(self, item_num):
del self.student[item_num - 1]
setattr(self, 'student', self.student)
print('删除成功')
print(self.student)
def aver_age(self):
sum_age = 0
for item in self.student:
sum_age += item['age']
aver_age = sum_age / len(self.student)
print('平均年龄为{}'.format(aver_age))
grade = ClassGrade(student=[])
num = 0
while True:
print(fileManage.read_text_file('page.txt'))
input_value = input('请选择你的操作')
if input_value == '1':
grade.add_stu(num)
elif input_value == '2':
item_num = 0
for item in grade.student:
item_num += 1
print(str(item_num) + '> ' + '姓名:{}, 学号:{}, 年龄{}'
.format(item['name'], item['id'], item['age']))
grade.del_stu(item_num)
elif input_value == '3':
input_name = input('点名:')
for item in grade.student:
if item['name'] == input_name:
student = Student(item['id'], item['name'], item['age'])
student.check_in()
student.show_info()
elif input_value == '4':
grade.aver_age()
elif input_value == 'q':
exit()