1.声明⼀个电脑类: 属性:品牌、颜⾊、内存⼤小 方法:打游戏、写代码、看视频
- a.创建电脑类的对象,然后通过对象点的⽅方式获取、修改、添加和删除它的属性
- b.通过attr相关⽅方法去获取、修改、添加和删除它的属性
class Computer:
def __init__(self, brand, color = '黑色', memory = 8, weight=2):
self.brand = brand
self.color = color
self.memory = memory
self.weight = weight
def game(self):
pass
def write_code(self):
pass
def look_video(self):
pass
computer = Computer('华硕')
# 1)通过对象点的方式获取、修改、添加和删除它的属性
# 获取
print(computer.brand)
# 修改
computer.color = '白色'
# 添加
computer.size = 14.7
# 删除
del computer.weight
# 2)通过attr相关⽅方法去获取、修改、添加和删除它的属性
# 获取
print(getattr(computer, 'brand'))
print(getattr(computer, 'color', '蓝色'))
# 修改
setattr(computer, 'brand', '联想')
# print(getattr(computer, 'brand'))
# 添加
setattr(computer, 'key', 'very good')
# 删除
delattr(computer, 'size')
# print(computer.__dict__) # 查看对象属性
2.声明⼀个人的类和狗的类:
- 狗的属性:名字、颜⾊色、年龄;方法:叫唤
- 人的属性:名字、年年龄、狗;方法:遛狗
- a.创建⼈人的对象⼩小明,让他拥有⼀一条狗⼤大⻩黄,然后让⼩小明去遛⼤大⻩黄
class Dog:
def __init__(self, name, color='黄色', age='2'):
self.name = name
self.color = color
self.age = age
def cry_out(self):
pass
class Peron:
def __init__(self, name: str, age=13):
self.name = name
self.age = age
self.dog = None
def walk_dog(self):
if self.dog:
print('{}天天都喜欢去遛{}'.format(self.name, self.dog.name))
else:
print('没有狗')
p = Peron('小明')
p.dog = Dog('大黄')
p.walk_dog()
3.声明一个圆类,自己确定有哪些属性和方法
- 属性:半径;方法:求面积,求周长
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
def distance(self, other):
"""
求当前点到另一个点的距离
:param other: 另一个点对象
:return: 点与点的距离
"""
a = self.x - other.x
b = self.y - other.y
return (a ** 2 + b ** 2) ** 0.5
class Round:
pi = 3.1415927
def __init__(self, radius: float, center: Point):
self.radius = radius
self.center = center
def area(self):
# 类的字段在使用的时候用类名加字段名
return Round.pi * self.radius ** 2
def perimeter(self):
return 2 * Round.pi * self.radius
def center_distance(self, other):
"""
求两个圆的圆心距
:param other: 另外一个圆
:return: 圆心距
"""
# self.center = Ponit(2, 2) 第一个圆的圆心
# other.center = Ponit(4, 4) 第一个圆的圆心
return self.center.distance(other.center)
class Line:
def __init__(self, start_point: Point, end_point: Point):
self.start_point = start_point
self.end_point = end_point
def length(self):
return self.start_point.distance(self.end_point)
r = Round(0, Point(2, 2))
print(r.center_distance(Round(10, Point(4, 4))))
line1 = Line(Point(100, 100), Point(0, 0))
print(line1.length())
4.创建⼀一个学⽣生类: 属性:姓名,年龄,学号;方法:答到,展示学⽣生信息
创建⼀一个班级类:属性:学⽣,班级名;方法:添加学⽣生,删除学生,点名, 求班上学生的平均年龄
from random import randint
class Student:
def __init__(self, name, stu_id='001', age=17):
self.name = name
self.age = age
self.stu_id = stu_id
self.is_in_classroom = randint(0, 1)
def answer_to(self):
if self.is_in_classroom == 1:
print('%s 到' % self.name)
def show(self):
print('姓名:{}, 年龄:{}, 学号:{}'.format(self.name, self.age, self.stu_id))
class Class:
def __init__(self, name):
self.students = []
self.name = name
def add_stu(self):
"""添加学生"""
stu_name = input('姓名:')
stu_age = input('年龄:')
stu_id = input('学号:')
new_stu = Student(stu_name, stu_id, int(stu_age))
self.students.append(new_stu)
def del_stu(self):
"""
删除学生
stu是学生对象
"""
stu_id = input('请输入需要删除的学生的学号:')
for stu in self.students[:]:
if stu.stu_id == stu_id:
self.students.remove(stu)
break
else:
print('没有该学生')
def roll_call(self):
for stu in self.students:
print(stu.name)
stu.answer_to()
def avg_stu(self):
count = len(self.students)
sum = 0
for stu in self.students:
sum += stu.age
print('平均年龄 %.2f' % (sum / count))
def sort_stu(self):
self.students.sort(reverse=True, key=lambda item: item.age)
def show_all_stu(self):
for stu in self.students:
stu.show()
cls = Class('Py1906')
for _ in range(5):
cls.add_stu() # 添加学生
print('====显示学生===')
cls.show_all_stu() # 显示学生
print('====排序====')
cls.sort_stu() # 排序
cls.show_all_stu() # 显示学生
print('====点名====')
cls.roll_call() # 点名
print('====平均年龄====')
cls.avg_stu() # 平均年龄
print('====删除学生====')
cls.del_stu() # 删除学生
cls.show_all_stu() # 显示学生