Python 类的继承实例详解
Python类的继承详解
Python既然是面向对象的,当然支持类的继承,Python实现类的继承比JavaScript简单。
Parent类:
classParent:
parentAttr=100
def__init__(self):
print("parentInit")
defparentMethod(self):
print("parentMethod")
defsetAttr(self,attr):
self.parentAttr=attr
defgetAttr(self):
print("ParentAttr:",Parent.parentAttr)
Child类
classChild(Parent):
def__init__(self):
print("childinit")
defchildMethod(self):
print("childMethod")
调用
p1=Parent(); p1.parentMethod(); c1=Child(); c1.childMethod();
输出:
parentInit parentMethod childinit childMethod Pressanykeytocontinue...
Python支持多继承
classA:#定义类A ..... classB:#定义类B ..... classC(A,B):#继承类A和B .....
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!