74.构造函数
class Foob:def __init__(self):(注意前面和后面分别都是两个下划线)self.name=66输出:>>> f=Foob()>>> f.name66
---------------------------class Foob:def __init__(self,value):self.name=value输出:>>> f=Foob(45)>>> f.name45>>> f=Foob('are you sure?')>>> f.name'are you sure?'
--------------------------------------------------------------------------------------
75.重写普通方法和特殊的构造函数
先举个例子:
class A: def hello(self): print("Hello, I'm A.")class B(A): pass
输出:>>> a=A()>>> b=B()>>> a.hello()Hello, I'm A.>>> b.hello()Hello, I'm A.
因为B是继承了A类的,所以在B中没有进行任何的添加功能的情况下,输出的结果还是A中的内容,但是现在可以重写,重写A中的内容,例如:
class A: def hello(self): print("Hello, I'm A.")class B(A): def hello(self): print("Hello, I'm B.")输出:>>> a=A()>>> b=B()>>> a.hello()Hello, I'm A.>>> b.hello()Hello, I'm B.
-----------------------------------------------------------------------------------