【AI达人创造营】面向对象-头脑风暴
收藏
# 面向对象-头脑风暴
## 1.用描述动物、人、猫、狗、大人、小孩
class Animal(object):
def __init__(self, name, age, weight, height):
self.name = name
self.age = age
self.weight = weight
self.height = height
def speak(self):
pass
def __str__(self):
return f"{self.name}的年龄:{self.age},体重:{self.weight},身高:{self.height}"
class Human(Animal):
def __init__(self, name, age, weight, height, race, color, legs=2):
super().__init__(name, age, weight, height)
self.race = race
self.color = color
self.legs = legs
def __str__(self):
return f"{self.name}的年龄:{self.age},体重:{self.weight},身高:{self.height}, 种族:{self.race}, 肤色:{self.color}, 腿数:{self.legs}"
class Adult(Human):
def speak(self):
print(f"{self.name}:大家好,我是一个成年人!\n")
class Kids(Human):
def speak(self):
print(f"{self.name}:wa wa wa, a a a...\n")
class Dog(Animal):
def __init__(self, name, age, weight, height, legs=4):
super().__init__(name, age, weight, height)
self.legs = legs
def __str__(self):
return f"{self.name}的年龄:{self.age},体重:{self.weight},身高:{self.height}, 腿数:{self.legs}"
def speak(self):
print(f"{self.name}: wang! wang! wang!\n")
class Cat(Animal):
def __init__(self, name, age, weight, height, legs=4):
super().__init__(name, age, weight, height)
self.legs = legs
def __str__(self):
return f"{self.name}的年龄:{self.age},体重:{self.weight},身高:{self.height}, 腿数:{self.legs}"
def speak(self):
print(f"{self.name}: miao! miao! miao!\n")
zhangsan = Adult('张三', 25, 130, 178, '汉', 'yellow')
print(zhangsan)
zhangsan.speak()
xiaoming = Kids('小明', 5, 40, 110, '汉', 'yellow')
print(xiaoming)
xiaoming.speak()
cat = Cat('小花猫', 2, 12, 30)
print(cat)
cat.speak()
dog = Dog('小黑', 2, 80, 70)
print(dog)
dog.speak()
## 2.我是一个老师,希望用类来描述一下我的班级,希望实现记录学生信息、学生成绩,并和学生互动(开小灶、询问成绩)
# 2.我是一个老师,希望用类来描述一下我的班级,希望实现记录学生信息、学生成绩,并和学生互动(开小灶、询问成绩)
class Student(object):
def __init__(self, name, age, height, weight, score):
self.name = name
self.age = age
self.height = height
self.weight = weight
self.score = score
def __str__(self):
return f'学生姓名:{self.name} 年龄:{self.age} 身高: {self.height} 体重:{self.weight} 语文成绩:{self.score["语文"]} ' \
f'数学成绩:{self.score["数学"]} 英语成绩:{self.score["英语"]}'
class MyClass(object):
def __init__(self, teacher_name, class_num=1, students=[]):
"""
:param teacher_name:
:param class_num:
:param students: 存储学生对象的列表
"""
self.teacher_name = teacher_name
self.class_num = class_num
self.__students = students # 设置为私有,不能在外部随意修改学生信息
def __str__(self):
return f'{self.teacher_name}管理的班级是{self.class_num},班里有{len(self.__students)}名学生.\n'
def kai_xiao_zao(self, student):
"""
开小灶
:param student: 需要传入一个 学生对象 作为参数
:return:
"""
print(f'{self.teacher_name}正在给{student.name}开小灶!\n')
def ask_for_scores(self, student):
"""
询问成绩
:param student: 需要传入一个 学生对象 作为参数
:return:
"""
print(f'{self.teacher_name}: {student.name}你这次考试考的怎么样?')
print(f"{student.name}: 我语文成绩为{student.score['语文']},数学成绩为{student.score['数学']},英语成绩为{student.score['数学']}")
def add_student(self, stu):
"""
向班级中增加学生
:param stu: 需要传入一个 学生对象 作为参数
:return:
"""
self.__students.append(stu)
# 创建一个班级,管理老师是王老师
my_class = MyClass('王老师')
# 创建三个学生
stu1 = Student('张三', 23, 178, 150, {'语文': 87, '数学': 90, '英语': 100})
stu2 = Student('李四', 20, 175, 138, {'语文': 98, '数学': 92, '英语': 99})
stu3 = Student('王五', 21, 178, 140, {'语文': 99, '数学': 78, '英语': 67})
# 将三个学生加入王老师班级
my_class.add_student(stu1)
my_class.add_student(stu2)
my_class.add_student(stu3)
# 打印王老师所带班级的情况
print(my_class)
# 王老师给学生开小灶
my_class.kai_xiao_zao(stu1)
# 王老师询问学生成绩
my_class.ask_for_scores(stu1)
0
收藏
请登录后评论