1.声明⼀个电脑类: 属性:品牌、颜⾊、内存⼤小 方法:打游戏、写代码、看视频
class Computer:
def __init__(self, brand, color, memory):
self.brand = brand
self.color = color
self.memory = memory
def game(self):
print('打游戏')
def code(self):
print('写代码')
def video(self):
print('看视频')
a.创建电脑类的对象,然后通过对象点的⽅方式获取、修改、添加和删除它的属性
computer = Computer(brand='Timi', color='gray', memory=16)
print(computer.brand)
computer.brand = 'Asus'
print(computer.brand)
computer.weight = 2
print(computer.weight)
del computer.weight
print(computer.weight)
b.通过attr相关⽅方法去获取、修改、添加和删除它的属性
computer1 = Computer(brand='Timi', color='gray', memory=16)
print(getattr(computer1, 'brand'))
setattr(computer1,'brand','Asus')
print(computer1.brand)
setattr(computer1,'weight',2)
print(computer1.weight)
delattr(computer1,'brand')
print(computer1.brand)
2.声明⼀个人的类和狗的类:
狗的属性:名字、颜色、年龄
狗的方法:叫唤
人的属性:名字、年龄、狗
人的⽅法:遛狗
a.创建⼈的对象⼩明,让他拥有⼀条狗⼤黄,然后让⼩明去遛⼤⻩
class Dog:
def __init__(self, name, color, age):
self.name = name
self.color = color
self.age = age
def bark(self):
print('%s:汪汪汪'%self.name)
class People:
def __init__(self, name, age, dog_name):
self.name = name
self.age = age
self.dog_name = dog_name
def walk_the_dog(self):
print('%s遛%s' %(self.name,self.dog_name))
dog1= Dog('大黄', 'yellow', 3)
people1 = People('小明', 18, dog1.name)
people1.walk_the_dog()
dog1.bark()
3.声明⼀一个圆类,自己确定有哪些属性和方法
from math import pi,sqrt
class Circle:
def __init__(self, radius, center):
self.radius = radius
self.center = center
def get_center(self):
print('圆的圆心为',self.center)
def get_radius(self):
print('圆的半径为',self.radius)
def set_center(self, center):
self.center = center
def set_radius(self, radius):
self.radius = radius
def area(self):
print('半径为%.2f的圆的面积为%.2f'%(self.radius,(pi * (self.radius **2))))
def perimeter(self):
print('半径为%.2f的圆的周长为%.2f'%(self.radius,(pi * 2 * self.radius)))
def center_distance(self):
print('圆心和原点的距离为%.2f'%(sqrt(self.center[0]**2 + self.center[1]**2)))
circle = Circle(3.1 ,(2,3))
circle.set_center((1,1))
circle.set_radius(2.9)
circle.get_center()
circle.get_radius()
circle.area()
circle.perimeter()
circle.center_distance()
4.创建⼀个学⽣生类:
属性:姓名,年龄,学号
方法:答到,展示学⽣信息
创建⼀个班级类:
属性:学⽣,班级名
方法:添加学生,删除学生,点名, 求班上学生的平均年龄
class Student:
def __init__(self, name, age, stu_id):
self.name = name
self.age = age
self.stu_id = stu_id
def answer(self):
print(self.name, self.age, self.stu_id)
class Class:
def __init__(self, student_list, class_name):
self.student_list = student_list
self.class_name = class_name
def add_student(self, student):
self.student_list.append(student)
print('%s成功添加!'%student.name)
def del_student(self, name):
for student in self.student_list[:]:
if student.name == name:
self.student_list.remove(student)
print('%s成功删除!'%name)
break
else:
print('%s不在名单中!'%name)
def call_name(self):
print('========%s点名========='%self.class_name)
for student in self.student_list[:]:
student.answer()
def ave_age(self):
sum = 0
for student in self.student_list[:]:
sum += student.age
print('%s的平均年龄为%.2f'%(self.class_name,(sum/len(self.student_list))))
s1 = Student('小明', 12 , 'py123')
s2 = Student('小红', 17 , 'py124')
s3 = Student('小王', 14 , 'py125')
s4 = Student('小张', 15 , 'py126')
s5 = Student('小杨', 11 , 'py127')
c1 = Class([s1,s2],'class1')
c1.add_student(s3)
c1.add_student(s4)
c1.add_student(s5)
c1.del_student('小红')
c1.call_name()
c1.ave_age()