1.声明⼀个电脑类: 属性:品牌、颜⾊、内存⼤小 方法:打游戏、写代码、看视频
a.创建电脑类的对象,然后通过对象点的⽅方式获取、修改、添加和删除它的属性
b.通过attr相关⽅方法去获取、修改、添加和删除它的属性
class Computer:
def __init__(self, brand, color, memory):
self.brand = brand
self.color = color
self.memory = memory
def gaming(self):
print('打游戏推荐%s牌,内存%s的电脑' % self.brand, self.memory)
def coding(self):
pass
def media_center(self):
pass
pc1 = Computer('dell', '32GB')
pc2 = Computer('hp', 'blue', '16GB')
pc3 = Computer('ALIENWARE', 'gray', '32GB')
print('===============a================')
print(pc1.name) # 查
pc1.name = 'thunder'
print(pc1.name) # 修改
pc1.color = 'black'
print(pc1.color) # 增
del pc1.brand
print(pc1.name) # 删
print('===============b================')
print(getattr(pc2, 'name')) # attr 方法查看
print(setattr(pc2, 'memory', 32)) # 修改
print(setattr(pc3, 'storage', 'SSD')) # 增加
delattr(pc3, 'color')
2.声明⼀个人的类和狗的类:
狗的属性:名字、颜⾊色、年年龄
狗的⽅方法:叫唤
人的属性:名字、年年龄、狗
人的⽅方法:遛狗
a.创建⼈人的对象⼩小明,让他拥有⼀一条狗⼤大⻩黄,然后让⼩小明去遛⼤大⻩黄
class Dog:
def __init__(self, name, color, age):
self.name = name
self.color = color
self.age = age
self.brand = '秋田犬'
def wanwan(self):
print('%s: wan-wan' % self.name)
class Human:
def __init__(self, name1, age1, own_dog):
self.name1 = name1
self.age1 = age1
self.own_dog = own_dog
def walk_the_dog(self):
print('%s在遛一条%s色的%s' % (self.name1, self.own_dog.color, self.own_dog.brand))
dog1 = Dog('丑丑', color='黄', age=5)
h1 = Humman('小明', 18, dog1)
h1.walk_the_dog()
3.声明⼀一个圆类,自己确定有哪些属性和方法
class Circle:
def __init__(self, radius, pi, center):
self.radius = radius
self.pi = pi
self.center = center
def circle_area(self):
print(self.pi * self.radius **2)
def circle_perimeter(self):
print(2*self.pi*self.radius)
def center_distance(self):
print('圆心和原点的距离为%.2f'%(sqrt(self.center[0]**2 + self.center[1]**2)))
c1 = Circle(4, 3.14)
c1.circle_area()
c1.circle_perimeter()
c1.center_distance()
4.创建⼀一个学⽣生类:
属性:姓名,年龄,学号
方法:答到,展示学⽣生信息
创建⼀一个班级类:
属性:学⽣生,班级名
方法:添加学⽣生,删除学生,点名, 求班上学生的平均年龄
class Student:
def __init__(self, name, age, stu_id):
self.name = name
self.age = age
self.stu_id = stu_id
def response(self):
print(self.name, self.age, self.stu_id)
class Class:
def __init__(self, stu_name, class_grade):
self.stu_name = stu_name
self.class_grade = class_grade
def add_stu(self, add_student):
self.stu_name.append(add_student)
print('%s添加成功' % add_student.name)
def del_stu(self, name):
for student in self.stu_name[:]:
self.stu_name.remove(student)
print('删除学生%s' % name)
break
def check_roll(self):
for student in self.stu_name[:]:
# print('到!%s今年%s岁,学号是%s' % self.name, self.age, self.stu_id)
student.response()
def stu_avg_age(self):
sum1 = 0
for student in self.stu_name[:]:
sum1 += student.age
print('%s班的平均成绩是%.2f' % (self.class_grade, sum(len(self.stu_name))))
stu1 = Student('王力宏', 19, 'py100110')
stu2 = Student('王自如', 21, 'py100112')
stu3 = Student('王石', 22, 'py100113')
stu4 = Student('王力', 19, 'py100114')
stu4 = Student('王欣', 18, 'py100115')
c1 = Class([stu1,stu2], 'class1')
c1.add_stu(stu1)
c1.add_stu(stu2)
c1.add_stu(stu3)
c1.del_stu('王欣')
c1.check_roll()
c1.stu_aug_age()