代码:
#!/user/nim/python
#-*-coding: UTF-8 -*-
class Test: #类
i=1 #公有变量
__a=0 #私有变量
def f(self): #def关键字定义方法,方法必须包含参数self,且self为第一个参数
return 'abc'
def __init__(self,real,image): #__init__为类的构造方法,在类实例化时自动调用
self.r=real
self.i=image
class DerivedTest(Test): #继承
def f(self):
return 'aa'
x=Test(3,4) #类对象
y=DerivedTest(1,2)
print "x:",x.i
print "f:",x.f()
print "real:",x.r," image:",x.i
print "y:",y.i
运行结果:
x: 4
f: abc
real: 3 image: 4
y: 2