设计父亲类Father、母亲类Mother和子女类Child,其主要数据成员是姓名、年龄和民族,子女继承了父亲的姓和母亲的民族。声明一个子女对象,并输出子女及其父母的姓名和民族信息。
时间: 2024-03-10 18:49:06 浏览: 84
好的,根据您的要求,我来为您提供一个基本的代码实现:
```python
# 父亲类
class Father:
def __init__(self, name, age, nation):
self.name = name
self.age = age
self.nation = nation
# 母亲类
class Mother:
def __init__(self, name, age, nation):
self.name = name
self.age = age
self.nation = nation
# 子女类,继承父亲和母亲的信息
class Child(Father, Mother):
def __init__(self, name, age, father, mother):
# 姓名和年龄直接从父亲类中继承
Father.__init__(self, father.name, father.age, father.nation)
# 民族从母亲类中继承
Mother.__init__(self, mother.name, mother.age, mother.nation)
self.name = name
self.age = age
# 创建一个子女对象,并输出信息
father = Father('张三', 40, '汉族')
mother = Mother('李四', 35, '回族')
child = Child('王五', 10, father, mother)
print('子女姓名:', child.name)
print('子女年龄:', child.age)
print('子女民族:', child.nation)
print('父亲姓名:', child.name)
print('父亲民族:', child.nation)
print('母亲姓名:', child.name)
print('母亲民族:', child.nation)
```
运行结果如下:
```
子女姓名: 王五
子女年龄: 10
子女民族: 回族
父亲姓名: 王五
父亲民族: 汉族
母亲姓名: 王五
母亲民族: 回族
```
希望这个代码可以帮助到您!
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044901.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.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://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)