老规矩不BB直接上代码
#coding:utf-8
class Parent(object):
def __init__(self,name,sex):
self.name=name
self.sex=sex
def haha(self):
return 'hhh'
def talk(self):
return f'{self.name}are walking'
def is_sex(self):
if self.sex=='boy':
return f'{self.name}is a boy'
else:
return f'{self.name}is a girl'
class childOne(Parent):
def play_fo(self):
return f'{self.name}are playing'
class childTwo(Parent):
def play_pi(self):
return f'{self.name}are play pingpong'
Parent=Parent('小胖','女')
print(Parent.haha())
print('--------------')
childOne=childOne('狗蛋','女')
result=childOne.play_fo()
print(childOne.talk())
print(result)
print('------------------')
childTwo=childTwo('妮妮','男')
result=childTwo.play_pi()
print(result)
super函数
super函数 其实就是子类去调用父类的方法
#coding:utf-8
class Parent(object):
def supreme(self,p):
print('hello i am supreme %s'% p)
class Child(Parent):
def __init__(self,c,p):
print('hello i am child %s'% c)
super(Child,self).supreme(p)
if __name__=='__main__':
c=Child('狗子','小芳') //hello i am child 狗子 hello i am supreme 小芳