保持清醒,不断思考。

Mike lucis


  • 首页
  • 归档
  • 分类
  • 标签
  • 关于
  • 联系
  •     

© 2023 Mike lucis

Theme Typography by Makito

Proudly published with Hexo

备案号: 湘ICP备2021005917号-1

友情链接: Lonely's Bolg

ღゝ◡╹)ノ♡

类的继承

发布于 2021-05-13 Python  Advance 

¶ 见Python源代码/注释

Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class Parent:  # 定义父类
parentAttr = 100

def __init__(self):
print("调用父类构造函数")

def parentMethod(self):
print('调用父类方法')

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

def getAttr(self):
print("父类属性 :", Parent.parentAttr)


class Child(Parent): # 定义子类
def __init__(self):
print("调用子类构造方法")

def childMethod(self):
print('调用子类方法')


c = Child() # 实例化子类
c.childMethod() # 调用子类的方法
c.parentMethod() # 调用父类方法
c.setAttr(200) # 再次调用父类的方法 - 设置属性值
c.getAttr() # 再次调用父类的方法 - 获取属性值

Run:

1
2
3
4
调用子类构造方法
调用子类方法
调用父类方法
父类属性 : 200

分享到 

 上一篇: 运算符重载 下一篇: 面对对象思想 

© 2023 Mike lucis

Theme Typography by Makito

Proudly published with Hexo

备案号: 湘ICP备2021005917号-1

友情链接: Lonely's Bolg

ღゝ◡╹)ノ♡