1.声明⼀个电脑类: 属性:品牌、颜⾊、内存⼤小 方法:打游戏、写代码、看视频
a.创建电脑类的对象,然后通过对象点的⽅方式获取、修改、添加和删除它的属性
b.通过attr相关⽅方法去获取、修改、添加和删除它的属性
import random
class Computer:
"""电脑类"""
def __init__(self, brand, color, ram):
self.brand = brand
self.color = color
self.ram = ram
def paly_game(self): # 有波浪号警告,说明选的方法不对
return '打游戏'
def write_code(self, lang: str):
return '写%s代码' % lang
def watch_video(self):
return '看视频'
c1 = Computer('华为', '白色', '512G')
print(c1.brand) # 华为
c1.color = '黑色'
print(c1.color) # 黑色
c1.price = '1000'
print(c1.price) # 1000
# del c1.price
# print(c1.price) # AttributeError: 'Computer' object has no attribute 'price'
print(getattr(c1, 'brand')) # 华为
setattr(c1, 'color', '红色')
print(c1.color) # 红色
setattr(c1, 'price', 200)
print(c1.price) # 200
# delattr(c1, 'price')
# print(c1.price) # AttributeError: 'Computer' object has no attribute 'price'
2.声明⼀个人的类和狗的类:
狗的属性:名字、颜⾊、年龄
狗的⽅方法:叫唤
人的属性:名字、年龄、狗
人的⽅方法:遛狗
a.创建⼈的对象小明,让他拥有一条狗大黄,然后让小明去遛大黄
class Dog:
"""狗"""
def __init__(self, name, color, age):
self.name = name
self.color = color
self.age = age
def shout(self):
print('%s在叫:汪汪汪' % self.name)
class Person:
"""人类"""
def __init__(self, name, age):
self.name = name
self.age = age # 拥有年龄
self.dog = None # 狗类的对象,具体的某只狗
def walk_the_dog(self):
if not self.dog:
print('没有狗')
else:
print('%s牵着%s在散步' % (self.name, self.dog.name))
p1 = Person('小明', 35) # 创建人的对象 ??没有声明self.dog就能用?
dog1 = Dog('大黄', '白色', 5) # 创建狗的对象
p1.dog = dog1 # 让人拥有狗
p1.walk_the_dog()
3.声明⼀圆类,自己确定有哪些属性和方法
class Circle:
pi = 3.1415926
def __init__(self, radius):
self.radius = radius
def perimeter(self):
return 2 * Circle.pi * self.radius
def area(self):
return Circle.pi * self.radius ** 2
c1 = Circle(3)
print(c1.perimeter())
print(c1.area())
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, class_name):
self.students = [] # 一个班有多个学生:一个列表,列表中的元素是学生
self.class_name = class_name
def func():
num = 1
while True:
yield 'stu'+str(num)
num += 1
self.creat_id = func() # 学号生成器
def add_student(self):
name = input('学生姓名;')
age = int(input('学生的年龄:'))
study_id = next(self.creat_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()
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的学生============')
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()