1.声明⼀个电脑类: 属性:品牌、颜⾊、内存⼤小 方法:打游戏、写代码、看视频
class Computer:
def __init__(self, product, color, memory):
self.product = product
self.color = color
self.memory = memory
def game(self):
return '%s can play games' % self.product
def write_code(self):
return '%s can write code' % self.product
def watch_movies(self):
return '%s can write code' % self.product
a.创建电脑类的对象,然后通过对象点的⽅方式获取、修改、添加和删除它的属性
compu = Computer('小米', 'red', '512G')
print(compu.product)
print(compu.memory)
compu.memory = '1T'
print(compu.memory)
compu.size = '14inch'
del compu.color
print(compu)
b.通过attr相关⽅方法去获取、修改、添加和删除它的属性
compu1 = Computer('小米', 'red', '512G')
print(getattr(compu1, 'name', " no attribution called 'name' "))
print(getattr(compu1, 'product', 'false'))
setattr(compu1, 'color', 'green')
setattr(compu1, 'size', '14inch')
delattr(compu1, 'memory')
print(compu1)
2.声明⼀个人的类和狗的类:
狗的属性:名字、颜⾊色、年年龄
狗的⽅方法:叫唤
人的属性:名字、年年龄、狗
人的⽅方法:遛狗
a.创建⼈人的对象⼩小明,让他拥有⼀一条狗⼤大⻩黄,然后让⼩小明去遛⼤大⻩黄
class Dog:
def __init__(self, name, color, age):
self.name = name
self.age = age
self.color = color
def bark(self):
return '%s会叫' % (self.name)
class People:
def __init__(self, name, age=20):
self.name = name
self.age = age
self.dog = None #狗的对象
def wolk_the_dog(self):
if not self.dog:
print('没有狗')
else:
print('%s正在溜%s' % (self.name, self.dog.name))
p1 = People('小明', 19 )
dog1 = Dog('大黄', 'red', 3)
p1.dog = dog1
p1.walk_the_dog()
3.声明⼀一个圆类,自己确定有哪些属性和方法
class Circle:
pai = 3.14
def __init__(self, r):
self.r = r
def perimeter(self):
return Circle.pai * self.r * 2
def area(self):
return Circle.pai * self.r ** 2
c = Circle(3)
print(c.area(), c.perimeter())
4.创建⼀一个学⽣生类:
属性:姓名,年龄,学号
方法:答到,展示学⽣生信息
创建⼀一个班级类:
属性:学⽣生,班级名
方法:添加学⽣生,删除学生,点名, 求班上学生的平均年龄
class Student:
def __init__(self, name, age=0, study_id='000'):
self.name = name
self.age = age
self.study_id = study_id
self.is_in_class = random.randint(0, 1)
def respond(self):
print('%s,到!' % self.name)
def show(self):
print('姓名:%s 年龄:%d 学号:%s' % (self.name, self.age, self.study_id))
class Class:
def __init__(self, name):
self.students = [] # 一个半有多个学生: 一个列表,列表中的元素是学生对象
self.name = name
def func():
num = 1
while True:
yield 'stu'+str(num)
num += 1
self.create_id = func() # 学号生成器
def add_student(self):
"""添加学生"""
name = input('学生姓名:')
age = int(input('学生的年龄:'))
study_id = next(self.create_id)
stu = Student(name, age, study_id)
self.students.append(stu)
def del_student(self):
"""删除学生"""
del_name = input('请输入要删除的学生的名字:')
flag = False
for stu in self.students[:]:
if stu.name == del_name:
flag = True
stu.show()
value = input('是否删除(y/n):')
if value == 'y':
self.students.remove(stu)
if not flag:
print('没有该学生!')
def call_name(self):
"""点名"""
for stu in self.students:
print(stu.name)
if stu.is_in_class:
stu.respond()
def avg_age(self):
"""求平均年龄"""
ages = 0
for stu in self.students:
ages += stu.age
return ages / len(self.students)
def show_all_students(self):
print('=========%s的学生========='%self.name)
for stu in self.students:
stu.show()
c1 = Class('py1903')
c2 = Class('py1904')
c1.add_student() # 001
c1.add_student() # 002
c2.add_student() # 001
c1.add_student() # 003
c1.show_all_students()
c2.show_all_students()
网友评论