1.声明一个电脑类: 属性:品牌、颜色、内存大小 方法:打游戏、写代码、看视频
class Computer:
def __init__(self, brand, color, memory):
self.brand = brand
self.color = color
self.memory = memory
def play_game(self):
print("打游戏!!!")
def write_code(self):
print("写代码!!!")
def see_video(self):
print("看视频!!!")
c1 = Computer('lenovo', '黑色', '4G')
a.创建电脑类的对象,然后通过对象点的方式获取、修改、添加和删除它的属性
# 获取属性
print(c1.brand, c1.color, c1.memory)
# 修改属性
c1.brand = 'apple'
c1.color = '灰色'
c1.memory = '6G'
print(c1.brand, c1.color, c1.memory)
# 添加属性
c1.size = 14.5
print(c1.size)
# 删除属性
del c1.brand
# print(c1.brand) # AttributeError:'Computer' object has no attribute 'brand'
b.通过attr相关方法去获取、修改、添加和删除它的属性
c2 = Computer('lenovo', '黑色', '4G')
# 获取属性
print(getattr(c2, 'brand'))
print(getattr(c2, 'color'))
print(getattr(c2, 'memory'))
# 修改属性
setattr(c2, 'brand', 'apple')
print(c2.brand)
setattr(c2, 'color', '灰色')
print(c2.color)
setattr(c2, 'memory', '6G')
print(c2.memory)
# 添加属性
setattr(c2, 'size', '16.5')
print(c2.size)
# 删除属性
delattr(c2, 'memory')
#print(c2.memory) # AttributeError:'Computer' object has no attribute 'memory'
2.声明一个人的类和狗的类:
狗的属性:名字、颜色、年龄
狗的方法:叫唤
人的属性:名字、年龄、狗
人的方法:遛狗
a.创建人的对象是小明,让他拥有一条狗大黄,然后让小明去遛大黄
class Dog:
def __init__(self, name, color, age):
self.name = name
self.color = color
self.age = age
def dog_barks(self):
print("汪汪汪~~~")
dog1 = Dog('大黄', '黄色', 1)
class Person:
def __init__(self, name, age, dog):
self.name = name
self.age = age
self.dog = dog
def walk_dog(self):
dog1.dog_barks()
p1 = Person('小明', 18, dog1.name)
print(p1.walk_dog())
3.声明一个圆类:
class Circle:
pi = 3.14
def __init__(self, radius):
self.radius = radius
def area(self):
return Circle.pi * self.radius**2
def perimeter(self):
return 2 * Circle.pi * self.radius
r1 = Circle(10)
print("圆的面积:%.2f; 圆的周长:%.2f" % (r1.area(), r1.perimeter()))
4.创建一个学生类:
属性:姓名,年龄,学号
方法:答到,展示学生信息
创建一个班级类:
属性:学生,班级名
方法:添加学生,删除学生,点名, 求班上学生的平均年龄
class The_class:
def __init__(self, class_name:str):
self.stu_name = []
self.class_name = class_name
def add_student(self, student):
# print("添加学生!")
self.stu_name.append(student)
def del_student(self, name): # 删除学生
for item in self.stu_name:
if name == item['name']:
self.stu_name.remove(item)
print("删除成功!")
break
else:
print("英雄不存在!")
def call_name(self, name):
print("%s" % name) # 点名
for item in self.stu_name:
if name == item['name']:
print("%s到!" % name)
break
else:
print("%s打怪去了" % name)
def age_average(self):
# print("班上学生的平均值!")
sum = 0
for item in self.stu_name:
sum += item['age']
return sum/len(self.stu_name)
class Student:
def __init__(self, name, age, id):
self.name = name
self.age = age
self.id = id
def answer(self):
print("%s到" % self.name)
def show_stu_information(self):
# print("展示学生信息!")
print("学生信息:学号:%s 姓名:%s 年龄:%d" % (self.id, self.name, self.age))
stu1 = Student("娜美", 18, "stu001")
stu2 = Student("寒冰", 17, "stu002")
stu3 = Student("妖姬", 30, "stu003")
stu4 = Student("赏金", 21, "stu004")
stu5 = Student("狐狸", 28, "stu005")
class1 = The_class("英雄联盟女人班")
class1.add_student(stu1.__dict__)
class1.add_student(stu2.__dict__)
class1.add_student(stu3.__dict__)
class1.add_student(stu4.__dict__)
class1.add_student(stu5.__dict__)
print(class1.class_name)
stu1.show_stu_information()
stu2.answer()
print("班上英雄平均年龄:%s" % class1.age_average())
class1.call_name("寒冰")
class1.call_name("妖姬")
class1.del_student("盖伦")
英雄联盟女人班
学生信息:学号:stu001 姓名:娜美 年龄:18
寒冰答到
班上英雄平均年龄:22.8
寒冰
寒冰到!
妖姬
妖姬到!
英雄不存在!