源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

Python 类的继承实例详解

  • 时间:2020-09-23 13:38 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:Python 类的继承实例详解
[b]Python 类的继承详解[/b] Python既然是面向对象的,当然支持类的继承,Python实现类的继承比JavaScript简单。 [b]Parent类:[/b]
class Parent: 
 
  parentAttr = 100 
 
  def __init__(self): 
    print("parent Init") 
 
  def parentMethod(self): 
    print("parentMethod") 
   
  def setAttr(self,attr): 
    self.parentAttr = attr 
 
  def getAttr(self): 
    print("ParentAttr:",Parent.parentAttr) 
[b]Child类[/b]
class Child(Parent): 
 
  def __init__(self): 
    print("child init") 
 
  def childMethod(self): 
    print("childMethod") 
[b]调用[/b]
p1 = Parent(); 
p1.parentMethod(); 
 
c1 = Child(); 
c1.childMethod(); 
[b]输出:[/b]
parent Init 
parentMethod 
child init 
childMethod 
Press any key to continue . . . 
[b]Python支持多继承[/b]
class A:    # 定义类 A 
..... 
 
class B:     # 定义类 B 
..... 
 
class C(A, B):  # 继承类 A 和 B 
..... 
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部