(问答题)
使用类完成
妈妈长得很漂亮,爸爸iq很高,儿子继承了他们有优良基因,并且还弹得一手好钢琴。
class Mother:
def __init__(self):
self.beauty = 100
class Father:
def __init__(self):
self.IQ = 200
class Son(Mother, Father):
def play_piano(self):
print("I can play the piano!")
son = Son()
print("Son的美丽指数:", son.beauty)
print("Son的智商:", son.IQ)
son.play_piano()
不知道你这个问题是否已经解决, 如果还没有解决的话: ##可变关键字传参举例:
def showconfig(**kwargs):
for k,v in kwargs.items();
print(’{}={}‘.format(k,v),end=',')
showconfig(a=23,b=32,c='1331',d='adafas')#输出都是字典 {'a':23,'b':32,'c':'1331'}
首先,我们可以创建一个类来表示人的基本信息,包括姓名、外貌和智商:
class Person:
def __init__(self, name, appearance, intelligence):
self.name = name
self.appearance = appearance
self.intelligence = intelligence
然后,我们可以继承Person
类创建一个儿子类,并添加一个方法来表示儿子的特长:
class Son(Person):
def __init__(self, name, appearance, intelligence, specialty):
super().__init__(name, appearance, intelligence)
self.specialty = specialty
接下来,我们可以创建一个妈妈和爸爸对象,并使用他们的信息来创建一个儿子对象:
mom = Person("妈妈", "漂亮", None)
dad = Person("爸爸", None, "智商很高")
son = Son("儿子", mom.appearance, dad.intelligence, "弹钢琴")
最后,我们可以打印出儿子的基本信息:
print("儿子的姓名:", son.name)
print("儿子的外貌:", son.appearance)
print("儿子的智商:", son.intelligence)
print("儿子的特长:", son.specialty)
输出结果:
儿子的姓名: 儿子
儿子的外貌: 漂亮
儿子的智商: 智商很高
儿子的特长: 弹钢琴
通过创建一个Person
类和一个继承Person
的Son
类,我们可以很方便地表示妈妈、爸爸和儿子的基本信息,并且能轻松地扩展其他成员的属性和特长。