class A(object): def __init__(self): self.name = "cui" def get_name(self): return self.name class B(A): def __init__(self): super(B,self).__init__() self.age = 12 b = B()
class A(object): def __init__(self): self.name = "zhang" def get_name(self): return self.name class B(A): def __init__(self,age): super(B,self).__init__() self.age = age b = B("san")
class A(object): def __init__(self,sex): self.name = "zhang" self.sex = sex def get_name(self): return self.name + self.sex class B(A): def __init__(self,sex,age): super(B,self).__init__(sex) self.age = age b = B("男",12)
class A(object): def __init__(self,name,sex): self.name = name self.sex = sex def get_name(self): return self.name + self.sex class B(A): def __init__(self,name,sex,age): super(B,self).__init__(name,sex) self.age = age b = B("zhang","男",12)
class A(object): def __init__(self,name,sex): self.name = name self.sex = sex def get_name(self): return self.name + self.sex class B(A): def __init__(self,name,sex,age): super(B,self).__init__(name,sex) self.age = age self.courage = "高中" b = B("zhang","男",12)
class A(object): def __init__(self,name): self.name = name self.sex = "女" def get_name(self): return self.name + self.sex class B(A): def __init__(self,name,sex,age): super(B,self).__init__(name) self.age = age self.courage = "高中" b = B("zhang","男",12)
子类继承父类的构造函数中,在子类的构造函数中,__init__方法中的变量是包括自己和父类所有需要传递的变量,而在super().__init__方法中的参数仅仅包括父类需要传递的变量
def __init__(self,name,sex,age): super(B,self).__init__(name) self.age = age self.courage = "高中"
以上就是python子类如何继承父类的实例变量的详细内容,更多关于python子类继承父类变量的资料请关注呐喊教程其它相关文章!
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:notice#nhooo.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。