1.声明⼀个电脑类: 属性:品牌、颜⾊、内存⼤小 方法:打游戏、写代码、看视频
a.创建电脑类的对象,然后通过对象点的⽅方式获取、修改、添加和删除它的属性
b.通过attr相关⽅方法去获取、修改、添加和删除它的属性
class Computer:
def __init__(self,brand,color,size):
self.brand = brand
self.color = color
self.size = size
@staticmethod
def play_game(game='LOL'):
return '可以玩'+game
@staticmethod
def write_code(code='python'):
return '可以写' + code
@staticmethod
def watch_video(video='一人之下'):
return '可以看' + video
lenovo = Computer('lenovo','black','4G')
print(lenovo.color)
lenovo.color = 'white'
print(lenovo.color)
lenovo.weight = '2KG'
print(lenovo.weight)
del lenovo.weight
print(getattr(lenovo,'weight',None))
print(getattr(lenovo,'color',None))
setattr(lenovo,'color','red')
print(getattr(lenovo,'color',None))
setattr(lenovo,'weight','3KG')
print(getattr(lenovo,'weight',None))
delattr(lenovo,'weight')
print(getattr(lenovo,'weight',None))
2.声明⼀个人的类和狗的类:
狗的属性:名字、颜⾊色、年年龄
狗的⽅方法:叫唤
人的属性:名字、年年龄、狗
人的⽅方法:遛狗
a.创建⼈人的对象⼩小明,让他拥有⼀一条狗⼤大⻩黄,然后让小明去遛大黄
class Dog:
def __init__(self,name,color,age):
self.name = name
self.color = color
self.age = age
@staticmethod
def bark():
return '汪汪汪'
class Person:
def __init__(self,name,age,dog=None): #形参类型为对象时,若要设置为空值,默认值写None
self.name = name
self.age = age
self.dog = dog
def walk_dog(self):
if self.dog:
person_name = self.name
dog_name = self.dog.name
return '%s在遛%s' % (person_name,dog_name)
else:
return '没有狗可以遛'
dog1 = Dog('大黄','yellow',2)
person1 = Person('小明',12,dog1)
print(person1.walk_dog())
3.声明⼀一个圆类,自己确定有哪些属性和方法
from math import pi
class Circle:
def __init__(self,radius:int):
self.radius = radius
def perimeter(self):
return '周长是%.2f' % (pi*self.radius*2)
def area(self):
return '面积是%.2f' % (pi*self.radius**2)
circle1 = Circle(3)
print(circle1.area())
print(circle1.perimeter())
4.创建⼀一个学⽣生类:
属性:姓名,年龄,学号
方法:答到,展示学⽣生信息
创建⼀一个班级类:
属性:学⽣生,班级名
方法:添加学⽣生,删除学生,点名, 求班上学生的平均年龄
class Student:
def __init__(self,name:str,age:int,number:str):
self.name = name
self.age = age
self.number = number
def show_info(self):
return '学生姓名:%s,年龄:%d,学号:%s' % (self.name,self.age,self.number)
class Class:
def __init__(self, class_name):
self.class_name = class_name
self.student_infos = []
def add_student(self, student):
self.student_infos.append(student)
return self.student_infos
def sub_student(self, student):
self.student_infos.remove(student)
return self.student_infos
def show_names(self):
names = ''
for item in self.student_infos:
names += item.name + ';'
return names
def average_age(self):
sum_age = 0
count = 0
for item in self.student_infos:
sum_age += item.age
count += 1
return sum_age / count
student1 = Student('A',14,'0001')
student2 = Student('B',16,'0002')
student3 = Student('C',15,'0003')
class1 = Class('三年二班')
class1.add_student(student1)
class1.add_student(student2)
class1.add_student(student3)
print(class1.average_age())
# a = [student1,student2,student3]
# print(a) 对象也可以作为列表元素
"""
class2 = Class('三年一班')
class2.add_student(student1)
class2.sub_student(student1)
print(class2.average_age())
"""
=============================================
class Student:
def __init__(self, name, age=0, num='0001'):
self.name = name
self.age = age
self.num = num
self.state = randint(0, 1) # 是否在教室的状态, 0 -> 不在, 1 -> 在
def amount(self):
print('%s 到!' % self.name)
def show(self):
print('姓名:%s 年龄:%d 学号:%s' % (self.name, self.age, self.num))
class Class:
def __init__(self, name):
self.students = [
Student('stu1', 19, '001'),
Student('stu2', 20, '002'),
Student('stu3', 23, '003'),
Student('stu1', 19, '004'),
Student('stu4', 30, '005'),
]
self.name = name
# 学号生成器
self.__nums = ('stu'+str(num).zfill(3) for num in range(50))
# 添加学⽣生
def add_student(self):
# 输入学生信息
stu_name = input('姓名:')
stu_age = int(input('年龄:'))
stu_num = next(self.__nums)
# 创建学生对象
stu = Student(stu_name, stu_age, stu_num)
# 将学生添加到班级中
self.students.append(stu)
# 删除学生
def del_student(self):
del_name = input('删除学生的姓名:')
for stu in self.students[:]:
if stu.name == del_name:
self.students.remove(stu)
# 点名
def call_the_roll(self):
for stu in self.students:
# 喊名字
print(stu.name)
# 如果在教室
if stu.state:
# 答到
stu.amount()
else:
print('....')
def average_age(self):
sum1 = 0
for stu in self.students:
sum1 += stu.age
average = sum1//len(self.students)
print('平均年龄: %d' % average)
return average
def cl_show(self):
for stu in self.students:
stu.show()
class1 = Class('py1901')
print('=========添加学生===========')
# for _ in range(5):
class1.add_student()
class1.cl_show()
print('=========删除学生===========')
class1.del_student()
class1.cl_show()
print('=========点名===========')
class1.call_the_roll()
class1.average_age()