- 声明一个电脑类
属性:品牌、颜色、内存大小
方法:打游戏、写代码、看视频
a.创建电脑类的对象,然后通过对象点的方式获取、修改、添加和删除它的属性
b.通过attr相关方法去获取、修改、添加和删除它的属性
class Computer:
"""创建一个电脑类"""
__slots__ = ('brand', 'color', 'rom', 'cpu', 'gpu', 'size', 'prize', 'heavy')
def __init__(self, brand, color, rom):
self.brand = brand
self.color = color
self.rom = rom
def play_game(self):
print('打游戏!')
def coding(self):
print('写代码!')
def watch_video(self):
print('看视频!')
ge60 = Computer('微星', 'black', '8GB')
ge60.brand = 'Alien'
print(ge60.brand) # Alien
my_rom = ge60.rom
print(my_rom) # 8GB
ge60.gpu = 'GTX760m'
print(ge60.gpu) # GTX760m
del ge60.gpu
# print(ge60.gpu) # AttributeError: gpu
# 查找
my_brand = getattr(ge60, 'brand')
print(my_brand) # Alien
my_color = ge60.__getattribute__('color')
print(my_color) # black
my_cpu = getattr(ge60, 'cpu', '该属性不存在')
print(my_cpu) # 该属性不存在
# 添加
setattr(ge60, 'cpu', 'i5')
print(getattr(ge60, 'cpu')) # i5
ge60.__setattr__('gpu', '1080Ti')
print(ge60.__getattribute__('gpu')) # 1080Ti
# 修改
setattr(ge60, 'cpu', 'i7')
print(getattr(ge60, 'cpu')) # i7
ge60.__setattr__('gpu', '1060Ti')
print(ge60.__getattribute__('gpu')) # 1060Ti
# 删除
delattr(ge60, 'cpu')
print(getattr(ge60, 'cpu', '该属性不存在')) # 该属性不存在
ge60.__delattr__('gpu')
print(getattr(ge60, 'gpu', '该属性不存在')) # 该属性不存在
2.声明一个人的类和狗的类:
狗的属性:名字、颜色、年龄
狗的 法:叫唤
人的属性:名字、 年龄、狗
人的方法:遛狗
a.创建人的对象名字叫小明,让他拥有一条狗 ,然后让小明去遛狗
class Dog:
"""创建一个狗的类"""
def __init__(self, name, color, age):
self.name = name
self.color = color
self.age = age
def bark(self):
print('汪汪汪!')
class Person:
"""人"""
def __init__(self, name, age, dog):
self.name = name
self.age = age
self.dog = dog
def walk_dog(self):
print('上街溜%s' % self.dog.name)
dog1 = Dog('旺财', '黄色', 2)
person1 = Person('小明', 18, dog1)
person1.walk_dog() # 上街溜旺财
dog1.bark() # 汪汪汪!
修正答案:
class Dog:
"""创建一个狗的类"""
def __init__(self, name, color, age):
self.name = name
self.color = color
self.age = age
def bark(self):
print('汪汪汪!')
class Person:
"""人"""
def __init__(self, name: str, age: int, dog: Dog=None):
self.name = name
self.age = age
self.dog = dog # 注意:dog属性的类型应该是Dog
def took_the_dog(self):
if self.dog:
print('%s正牵着%s在散步' % (self.name, self.dog.name))
else:
print('溜自己!')
def beat(self):
if self.dog:
print('%s在打自己的狗%s' % (self.name, self.dog.name))
self.dog.bark()
else:
print('对不起,你没有狗,打自己吧!')
dog1 = Dog('旺财', '黄色', 2)
person1 = Person('小明', 18, dog1)
person1.took_the_dog()
dog1.bark() # 汪汪汪!
person1.beat()
dog2 = Dog('大黄', '黑色', 2)
person2 = Person('小花', 18)
print(person2.dog)
person2.took_the_dog()
dog2.bark() # 汪汪汪!
person2.beat()
# 小明正牵着旺财在散步
# 汪汪汪!
# 小明在打自己的狗旺财
# 汪汪汪!
# None
# 溜自己!
# 汪汪汪!
# 对不起,你没有狗,打自己吧!
3.声明一个矩形类:
属性: 长、宽
方法:计算周长和面积
a.创建不同的矩形,并且打印其周长和面积
class Rectangle:
"""矩形"""
def __init__(self, length=1, width=1):
self.length = length
self.width = width
# 计算周长
def get_perimeter(self):
self.perimeter = 2 * self.length + 2 * self.width # 此处将周长添加为了属性,也可以不加self,算好后返回值并销毁变量
print(self.perimeter)
return self.perimeter
# 计算面积
def get_area(self):
self.area = self.length * self.width # 此处将面积添加为了属性,也可以不加self,算好后返回值并销毁变量
print(self.area)
return self.area
rectangle1 = Rectangle(width=10)
rectangle2 = Rectangle(5)
rectangle3 = Rectangle(2, width=20)
rectangle1.get_perimeter() # 22
rectangle1.get_area() # 10
rectangle2.get_perimeter() # 12
rectangle2.get_area() # 5
rectangle3.get_perimeter() # 44
rectangle3.get_area() # 40
4.创建一个学生类:
属性:姓名,年龄,学号,成绩
方法:答到,展示学生信息
创建一个班级类: 属性:学生,班级名
方法:添加学生,删除学生,点名, 获取班级中所有学生的平均值, 获取班级中成绩最好的学生
class Student:
"""学生"""
def __init__(self, name, age, stu_id, score):
self.name = name
self.age = age
self.stu_id = stu_id
self.score = score
def check_in(self):
print('%s到!' % self.name)
def show_info(self):
print('姓名:%s, 年龄:%s, 学号:%s, 成绩:%s'\
% (self.name, self.age, self.stu_id, self.score))
class Class:
"""班级"""
# 创建班级属性,学生用不定长参数方式创建,并且为Student类的对象
def __init__(self, class_name, *students):
self.class_name = class_name
self.students = list(students)
def add_stu(self, student):
self.students.append(student)
def del_stu(self, student):
self.students.remove(student)
def check_name(self):
for stu in self.students:
print(stu.name + '!')
stu.check_in()
def get_average_score(self):
sum1 = 0
for stu in self.students:
sum1 += stu.score
avg = sum1/len(self.students)
print(avg)
return avg
def get_best_score_stu(self):
best_score_stu = max(self.students, key=lambda item: item.score)
best_score_stu.show_info()
return best_score_stu
stu1 = Student('小明', 20, 'stu001', 90)
stu2 = Student('小花', 18, 'stu002', 88)
stu3 = Student('小张', 22, 'stu003', 95)
stu4 = Student('小李', 21, 'stu004', 87)
class1 = Class('py1807', stu1, stu2, stu3)
class1.check_name()
class1.get_average_score()
class1.get_best_score_stu()
class1.add_stu(stu4)
class1.del_stu(stu3)
class1.check_name()
class1.get_average_score()
class1.get_best_score_stu()
# 小明!
# 小明到!
# 小花!
# 小花到!
# 小张!
# 小张到!
# 91.0
# 姓名:小张, 年龄:22, 学号:stu003, 成绩:95
# 小明!
# 小明到!
# 小花!
# 小花到!
# 小李!
# 小李到!
# 88.33333333333333
# 姓名:小明, 年龄:20, 学号:stu001, 成绩:90
老师答案:
class Student:
def __init__(self, name='', age=0, score=0, study_id=''):
self.name = name
self.age = age
self.study_id = study_id
self.score = score
def replied(self):
print('%s,到!' % self.name)
def show_message(self):
print(self.__dict__)
import random
class Class:
# 类字段
__creat_id = ('python'+str(x).rjust(3, '0') for x in range(1, 101))
def __init__(self, name):
self.students = []
self.name = name
# 添加学生
def add_student(self):
# 输入学生信息
name = input('姓名:')
age = int(input('年龄:'))
id = next(Class.__creat_id)
score = random.randint(0, 100)
# 创建学生对象
stu = Student(name, age, score, id)
self.students.append(stu)
print('添加成功:')
stu.show_message()
# 删除学生
def del_student(self):
del_name = input('姓名:')
count = len(self.students) # 删除前学生的个数
for stu in self.students.copy():
if stu.name == del_name:
self.students.remove(stu)
if count == len(self.students):
print('没有该学生!')
def call(self):
for stu in self.students:
print(stu.name)
# 学生答到
stu.replied()
def average_score(self):
scores = 0
for stu in self.students:
scores += stu.score
return scores/len(self.students)
def most_excellent_student(self):
return max(self.students, key=lambda stu: stu.score)
class1 = Class('python1807')
for _ in range(5):
class1.add_student()
网友评论