练习题:
""" 题目一: 封装一个学员类StudentStudy 属性:姓名,年龄、所在城市、期望薪资 方法一:打印学员正在学习的课程,课程作为参数传递。 XXX学员正在学习XXX课程 方法二:打印学员的期望薪资。XXX学员学完后的期望薪资为XXX 实例化2个学员,分别调用方法一和方法二 题目二: 封装一个员工类Employee: 属性:员工姓名、工作年限、户籍城市、薪资、岗位名称 方法一:计算员工的一年薪资总额(不用含年终奖) 方法二:打印员工的姓名和工作年限: 员工XXX 工作年限为 XX (通过self访问员工名字和员工工作年限) 实例化2个员工,分别调用方法一(打印员工的年度薪资总额)和方法二 题目三: 挑战题(可留到周三学完之后再做!!) 封装一个学生类Student, - 属性:姓名,年龄,性别,英语成绩,数学成绩,语文成绩, 方法一:计算总分, 方法二:计算三科平均分 方法三:打印学生的个人信息:我的名字叫XXX,年龄:xxx,性别:xxx。 请定义此类,并实例化2个学生,并打印每个学生的个人信息,计算总分、计算平均分! """
答案如下:
print('=================== 题目一 ========================') class StudentStudy: def __init__(self, name, age, city, expected_salary): self.name = name self.age = age self.city = city self.expected_salary = expected_salary def study(self, subject): print(f'{self.name}正在学习{subject}课程') def offer_salary(self): print(f'{self.name}学员学完后的期望薪资为{self.expected_salary}') s1 = StudentStudy('sky', '18', '深圳', 15000) s1.study('python自动化测试') s2 = StudentStudy('jack', '25', '杭州', 2500) s2.offer_salary() print('\n=================== 题目二 ========================') class Employee: def __init__(self, name, working_years, household_register, salary, job_name): self.name = name self.working_years = working_years self.household_register = household_register self.salary = salary self.job_name = job_name def year_salery(self): print(f'{self.name}的年薪为:{self.salary * 12}') def print_employee_info(self): print(f'员工{self.name},工作年限为:{self.working_years}') e1 = Employee('sky', '5', '湖南', 15000, 'python自动化测试') e1.year_salery() e2 = Employee('jack', '35', '杭州', 2500, '卖东西') e2.print_employee_info() print('\n=================== 题目三 ========================') class Student: def __init__(self, name, age, gender, english_score, math_score, chinese_score): self.name = name self.age = age self.gender = gender self.english_score = english_score self.math_score = math_score self.chinese_score = chinese_score def all_score(self): scores = self.english_score + self.math_score + self.chinese_score print(f'{self.name}的总分为:{scores}') return scores def average_score(self): scores = self.all_score() print(f'{self.name}的平均分为:{scores/3}') def print_student_info(self): print(f'我的名字叫:{self.name},年龄:{self.age},性别:{self.gender}') s1 = Student('jack', 45, '男', 50, 60, 70) s1.print_student_info() s1.average_score() s2 = Student('张爱玲', 25, '女', 50, 60, 90) s2.print_student_info() s2.average_score()
执行结果如下:
D:\SkyWorkSpace\WorkSpace\Pytest\Temp\day09\venv\Scripts\python.exe D:/SkyWorkSpace/WorkSpace/Pytest/Temp/day09/test_05/py43-day11-sky-20210817.py =================== 题目一 ======================== sky正在学习python自动化测试课程 jack学员学完后的期望薪资为2500 =================== 题目二 ======================== sky的年薪为:180000 员工jack,工作年限为:35 =================== 题目三 ======================== 我的名字叫:jack,年龄:45,性别:男 jack的总分为:180 jack的平均分为:60.0 我的名字叫:张爱玲,年龄:25,性别:女 张爱玲的总分为:200 张爱玲的平均分为:66.66666666666667 Process finished with exit code 0