ref
http://www.runoob.com/python/python-object.html
note
#!/usr/bin/python
# -*- coding: UTF-8 -*-
class Employee:
'所有员工的基类'
empCount = 0
def __init__(self, name, salary):
self.name = name
self.salary = salary
Employee.empCount += 1
def displayCount(self):
print "Total Employee %d" % Employee.empCount
def displayEmployee(self):
print "Name : ", self.name, ", Salary: ", self.salary
__init__()方法是一种特殊的方法,被称为类的构造函数或初始化方法
当创建了这个类的实例时就会调用该方法
self代表类的实例,而非类

0320 更新 : 我们需要给 class 的方法加上self 参数, 而对函数,不用加这个强制参数

class outer1():
sess_hdl = 0
def __init__(self):
print "out1 init"
def out_me1(self):
sess_hdl = "string 0"
print sess_hdl
class outer2():
def __init__(self):
print "out2 init"
def out_me2(self):
t = outer1()
print t.sess_hdl
t.out_me1()
print t.sess_hdl
# 以上三行,依次打印出:
#0
#string 0
#0
print "outer2 call 1 done"
def main():
print "main start"
o2 = outer2()
o2.out_me2()
if __name__ == '__main__':
main()
output:

版权声明:本文为paulkg12原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。