class Cat: def __init__(self,_name): self.name = _name def eat(self): print("i am eating .") def show(self): print("name is %s"%self.name)tom = Cat("tom")tom.show()print("------------");lanmao = Cat("lanpang")lanmao.show()
python类的创建过程1.分配内存空间2.调用__init__方法,并且传递参数3.将地址返回给变量
__init__函数的使用,使得python中的类更加接近于c++中的类,通过在__init__函数中定义变量达到在类中统一定义属性的目的