1.类中初始化变量
class dig():
def __init__(self,x,y):
self.x=x
self.y=y
def pd(self,r):
s=self.x+self.y+z+r #self.z=self.x+self.y+self.r AttributeError: dig instance has no attribute 'r'
f=self.x+self.y+y+r #NameError: global name 'y' is not defined print s
print f
pdc=dig(1,3)
pdc.pd(4)
__init__方法中初始化的变量,为赋值时。实例化时必须赋值
2.类的继承
z=2
class dig:
def __init__(self,x,y):
self.x=x
self.y=y
def pd(self,r):
s=self.x+self.y+z+r #self.z=self.x+self.y+self.r AttributeError: dige20instance has no attribute 'r'
print s
class pdg(dig):
def __init__(self,x,y,j):
dig.__init__(self,x,y) #父类中变量需初始化,子类中没有__init__函数,子类也会自动继承父类中的变量(属性)
self.j=j
'''
def __init__(self,x,y,j):
dig.__init__(self,x,y)
self.j=j
'''
def jc(self,j):
dig.pd(self,j)
pdd=pdg(1,2,4)
pdd.jc(9)
14
[Finished in 4.1s]