Quick actions

cmd+k|ctrl+k

Navigation

Languages

Python 3 对象:3

Snippet info

Language

Python

Visibility

public

Author

fenghestudio

Created

2019-11-24T07:40:49Z

Updated

2019-11-24T07:40:49Z

#!/usr/bin/python3

class Parent:        # 定义父类
   parentAttr = 100
   def __init__(self):
      print ("Calling parent constructor")

   def parentMethod(self):
      print ('Calling parent method')

   def setAttr(self, attr):
      Parent.parentAttr = attr

   def getAttr(self):
      print ("Parent attribute :", Parent.parentAttr)

class Child(Parent): # 定义子类
   def __init__(self):
      print ("Calling child constructor")

   def childMethod(self):
      print ('Calling child method')

c = Child()          # 实例化子类
c.childMethod()      # 调用实例方法
c.parentMethod()     # 在子类实例中调用父类方法
c.setAttr(200)       # 在子类实例中调用父类方法
c.getAttr()          # 在子类实例中调用父类方法
INFO