创建一个生日类,该类包含年和月属性;创建学生类,其中包含生日类对象,设计学生类其余属性及函数,最终输出学生所有信息。
时间: 2023-06-14 15:06:11 浏览: 93
下面是一个基本的实现示例:
```python
class Birthday:
def __init__(self, year, month):
self.year = year
self.month = month
class Student:
def __init__(self, name, id, gender, birthday):
self.name = name
self.id = id
self.gender = gender
self.birthday = birthday
def get_info(self):
print("Name:", self.name)
print("ID:", self.id)
print("Gender:", self.gender)
print("Birthday:", self.birthday.year, self.birthday.month)
# 创建生日对象
birthday = Birthday(2000, 5)
# 创建学生对象
student = Student("Tom", "001", "male", birthday)
# 输出学生信息
student.get_info()
```
输出结果:
```
Name: Tom
ID: 001
Gender: male
Birthday: 2000 5
```
在上面的示例中,我们首先定义了一个生日类 Birthday,该类包含年 year 和月 month 属性。然后,我们定义了一个学生类 Student,其中包含生日类对象 birthday,以及学生的其他属性和函数。最后,我们创建了一个生日对象和一个学生对象,并输出学生的所有信息。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![.rar](https://img-home.csdnimg.cn/images/20210720083646.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![whl](https://img-home.csdnimg.cn/images/20210720083646.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)