python 类之间的参数传递

练手记录以及调试步骤.

class A(object):
    def __init__(self,a="A"):
        print("enter",a)
        print("leave",a)
    def mainA(readA,*D):   #如果使用实例化B(),调用mainA,D收集进来D多余的参数.(参考第3点说明)
        print("enter mainA")
        reaA = "内部添加的"
        print("leave mainA")
        return (reaA + "   "+str(D))        
    
class B(A):
    def __init__(self, a =None):
        print("enter B ")
        super().__init__()
        self.read = a 
        print("leave B")
    def main(read,*two,**three):  #这个read 与上一个self.read 无任何关联,星号收集参数.
        print("enter main")
        read = 1
        print("leave main")
        return read
    
'''
=================不实例化的调用,可以直接使用 类名.函数名(参数)   ======================

>>> B.main(123)
enter main
leave main
1
>>> fo=B.main(123)
enter main
leave main
>>> print(fo)
1
>>> 
'''


'''
==================不实例化继承后的A,也可以直接使用======================================
>>> print(B.mainA(12))
enter mainA
leave mainA
内部添加的   ()
>>>
'''





'''
=================================第3点======================================


细节方面注意:
类实例化后,如: x=B()    使用mainA时需要注意有多余的参数


>>> x=B()
enter B 
enter A
leave A
leave B
>>> x.mainA()
enter mainA
leave mainA
'内部添加的   ()'   #这里有一个多余的None 值
>>> 
'''

 

你可能感兴趣的:(Python)