- 声明一个电脑类
属性:品牌、颜色、内存大小
方法:打游戏、写代码、看视频
a.创建电脑类的对象,然后通过对象点的方式获取、修改、添加和删除它的属性
b.通过attr相关方法去获取、修改、添加和删除它的属性
class Computer:
def __init__(self, brand, color, size):
self.brand = brand
self.color = color
self.size = size
def play_game(self):
print('打游戏')
def write_code(self):
print('写代码')
def see_move(self):
print('看视频')
computer1 = Computer('华硕', '黑色', "12GB")
# 1.获取
print("a.品牌:%s 颜色:%s 内存大小:%s" % (computer1.brand, computer1.color, computer1.size))
print("b.品牌:%s 颜色:%s 内存大小:%s" % (getattr(computer1, "brand"), getattr(computer1, "color"), getattr(computer1, "size")))
# 2.修改
computer1.brand = "外星人"
setattr(computer1, 'color', '白色')
print("品牌:%s, 颜色:%s" % (computer1.brand, computer1.color))
# 3.添加
computer1.disc = '1T'
setattr(computer1, 'digit', '64')
print("磁盘大小:%s 位数:%s" %(computer1.disc, computer1.digit))
# 4.删除
del computer1.brand
delattr(computer1, 'color')
2.声明一个人的类和狗的类:
狗的属性:名字、颜色、年龄
狗的 法:叫唤
人的属性:名字、 年龄、狗
人的方法:遛狗
a.创建人的对象名字叫小明,让他拥有一条狗 ,然后让小明去遛狗
class Dog:
def __init__(self, name, color, age):
self.name = name
self.color = color
self.age = age
def call(self):
print('汪汪汪')
class Person:
def __init__(self, name, age, dog):
self.name = name
self.age = age
self.dog = dog
def wolk(self):
print('溜%s' % self.dog.name)
dog1 = Dog('二哈', '黑色', '2')
person1 = Person('小明', '20', dog1)
person1.wolk()
3.声明一个矩形类:
属性: 长、宽
方法:计算周长和面积
a.创建不同的矩形,并且打印其周长和面积
class Rectangle:
def __init__(self, width, length):
self.width = width
self.length = length
def perimeter(self):
per = (int(self.length) + int(self.width)) * 2
print('周长 %s' % per)
def area(self):
ar = int(self.length) * int(self.width)
print('面积 %s' % ar)
rectangle1 = Rectangle(40, 50)
rectangle1.perimeter()
rectangle1.area()
rectangle2 = Rectangle(60, 20)
rectangle2.perimeter()
rectangle2.area()
rectangle3 = Rectangle(20, 40)
rectangle3.perimeter()
rectangle3.area()
4.创建一个学生类:
属性:姓名,年龄,学号,成绩
方法:答到,展示学生信息
创建一个班级类: 属性:学生,班级名
方法:添加学生,删除学生,点名, 获取班级中所有学生的平均值, 获取班级中成绩最好的学生
class Student:
def __init__(self, name, age, sno, score):
self.name = name
self.age = age
self.sno = sno
self.score = score
def answer(self):
print('%s 到!' % self.name)
def show_info(self):
print("姓名:%s 年龄:%d 学号:%s 成绩:%s" % (self.name, self.age, self.sno, self.score))
class Class:
def __init__(self, name, student_list: list):
self.name = name
self.student_list = student_list
def add_student(self, student):
for item in self.student_list:
if item.sno == student.sno:
print("学号已经存在")
return
self.student_list.append(student)
def detele_student(self, sno):
for item in self.student_list:
if item.snp == sno:
self.student_list.remove(item)
break
else:
print('该学生不存在')
def avg(self):
i = 0
for item in self.student_list:
i += item.score
i /= len(self.student_list)
print('分数平均值:', i)
def max(self):
max1 = max(self.student_list, key=lambda item: item.score)
max1.show_info()
def call_student(self, s_name):
for item in self.student_list:
if item.name == s_name:
item.answer()
break
else:
print('没有此人')
student1 = Student('小明', 20, 'python001', 99)
student2 = Student('小红', 18, 'python002', 87)
student3 = Student('小花', 22, 'python003', 93)
student_list = [student1, student2, student3]
class1 = Class('python', student_list)
class1.avg()
class1.max()
class1.call_student('小明')
class1.detele_student('python001')
class1.call_student('小明')
class1.call_student('小王')
student4 = Student('小王', 23, 'python004', 83)
class1.add_student(student4)
class1.call_student('小王')
网友评论