1.声明1个电脑类: 属性:品牌、颜色、内存大小 方法:打游戏、写代码、看视频
class Computer:
def __init__(self, brand, color, ram):
self.brand = brand
self.color = color
self.ram = ram
@staticmethod
def play_games():
print('电脑可以玩游戏')
@staticmethod
def write_code():
print('电脑可以写代码')
@staticmethod
def watch_TV():
print('电脑可以看电视')
a.创建电脑类的对象,然后通过对象点的方式获取、修改、添加和删除它的属性
comp1 = Computer('Apple', 'gray', '16g')
print(comp1.brand)
comp1.color = 'black'
print(comp1.color)
comp1.ssd = '128g'
print(comp1.ssd)
del comp1.ram
comp1.play_games()
b.通过attr相关方法去获取、修改、添加和删除它的属性
comp2 = Computer('dell', 'red', '8g')
print(getattr(comp2, 'brand'))
setattr(comp2, 'color', 'white')
print(comp2.color)
setattr(comp2, 'ssd', '256g')
print(comp2.ssd)
delattr(comp2, 'ram')
comp2.watch_TV()
2.声明1个人的类和狗的类:
狗的属性:名字、颜色、年龄
狗的方法:叫唤
人的属性:名字、年龄、狗
人的方法:遛狗
创建人的对象小明,让他拥有一条狗大黄,然后让小明去遛大黄
class Dog:
def __init__(self, name, age, color):
self.name = name
self.age = age
self.color = color
def bark(self):
print('%s汪汪汪!' % self.name)
class Person:
def __init__(self, name, age, dog=None):
self.name = name
self.age = age
self.dog = dog
def liu_gou(self):
if self.dog:
print('%s遛%s' % (self.name, self.dog.name))
else:
print('没有狗!')
dog1 = Dog('大黄', '3', 'white')
person1 = Person('小明', '18')
person1.dog = dog1
person1.liu_gou()
3.声明一个圆类,自己确定有哪些属性和方法
class Circle:
def __init__(self, radius):
self.radius = radius
def area(self):
return self.radius ** 2 * 3.14
def perimeter(self):
return self.radius * 2 * 3.14
cir = Circle(5)
print(cir.area())
print(cir.perimeter())
4.创建一个学生类:
属性:姓名,年龄,学号
方法:答到,展示学生信息
创建一个班级类:
属性:学生,班级名
方法:添加学生,删除学生,点名, 求班上学生的平均年龄
from random import randint
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()