首页 > 编程语言 > 详细

python-class(3)

时间:2017-09-05 09:43:04      阅读:309      评论:0      收藏:0      [点我收藏+]
 1 #!/usr/bin/env python
 2 #-*- coding:utf-8 -*-
 3 ############################
 4 #File Name: class3.py
 5 #Author: frank
 6 #Email: frank0903@aliyun.com
 7 #Created Time:2017-09-04 14:55:16
 8 ############################
 9 
10 class Parent:
11     parentAttr = 100
12     def __init__(self):
13         print "invoke construct of base class"
14 
15     def parentMethod(self):
16         print "invode parentMethod"
17 
18     def setAttr(self, attr):
19         Parent.parentAttr = attr
20 
21     def getAttr(self):
22         print "attr of base class:", Parent.parentAttr
23 
24     def myMethod(self):
25         print 调用父类方法
26 
27 class Child(Parent):
28     def __init__(self):
29         print "invoke construct of sub class"
30         
31     def childMethod(self):
32         print "invode childMethod"
33 
34     def myMethod(self):
35         print 调用子类方法
36 
37 c = Child()          # 实例化子类
38 c.childMethod()      # 调用子类的方法
39 c.parentMethod()     # 调用父类方法
40 c.setAttr(200)       # 再次调用父类的方法 - 设置属性值
41 c.getAttr()          # 再次调用父类的方法 - 获取属性值
42 c.myMethod()

 

python-class(3)

原文:http://www.cnblogs.com/black-mamba/p/7476803.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!