1、定义学生类:有学号、性别、年龄;方法有:初始化值的方法、输出值的方法、就业的方法。 2、定义学生类的子类:大学生类,该子类有专业技能属性,就业岗位为:专业人员,工资起薪:4000元。输出大学生信息。 3、定义学生类的子类:高中生,爱好属性,及爱好所获得的成绩,就业岗位为:服务行业,工资起薪:2000元。输出高中生信息。 4、定义公司类,利用多态,实现公司雇佣学生,雇佣是给就业学生岗位和工资。实现雇佣高中生、大学生。
时间: 2023-11-28 12:47:06 浏览: 34
1. 定义学生类
```python
class Student:
def __init__(self, student_id, gender, age):
self.student_id = student_id
self.gender = gender
self.age = age
def output(self):
print("学号:{},性别:{},年龄:{}".format(self.student_id, self.gender, self.age))
def employment(self, job_title, salary):
print("就业岗位:{},工资起薪:{}元".format(job_title, salary))
```
2. 定义大学生类
```python
class CollegeStudent(Student):
def __init__(self, student_id, gender, age, major_skill):
super().__init__(student_id, gender, age)
self.major_skill = major_skill
def output(self):
super().output()
print("专业技能:{}".format(self.major_skill))
def employment(self):
super().employment("专业人员", 4000)
```
3. 定义高中生类
```python
class HighSchoolStudent(Student):
def __init__(self, student_id, gender, age, hobby, hobby_score):
super().__init__(student_id, gender, age)
self.hobby = hobby
self.hobby_score = hobby_score
def output(self):
super().output()
print("爱好:{},爱好成绩:{}".format(self.hobby, self.hobby_score))
def employment(self):
super().employment("服务行业", 2000)
```
4. 定义公司类
```python
class Company:
def hire(self, student):
student.employment()
```
使用示例:
```python
college_student = CollegeStudent("001", "男", 22, "Python编程")
college_student.output() # 学号:001,性别:男,年龄:22,专业技能:Python编程
company = Company()
company.hire(college_student) # 就业岗位:专业人员,工资起薪:4000元
high_school_student = HighSchoolStudent("002", "女", 18, "游泳", 90)
high_school_student.output() # 学号:002,性别:女,年龄:18,爱好:游泳,爱好成绩:90
company.hire(high_school_student) # 就业岗位:服务行业,工资起薪:2000元
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)